Skip to content

Instantly share code, notes, and snippets.

@yue1123
Last active April 19, 2024 15:39
Show Gist options
  • Select an option

  • Save yue1123/a4c73e45eda8e180f033875e3d43fb85 to your computer and use it in GitHub Desktop.

Select an option

Save yue1123/a4c73e45eda8e180f033875e3d43fb85 to your computer and use it in GitHub Desktop.
Determining whether a browser supports an event
function is( obj, type ) {
return typeof obj === type;
}
var isEventSupported = (function() {
var TAGNAMES = {
'select': 'input', 'change': 'input',
'submit': 'form', 'reset': 'form',
'error': 'img', 'load': 'img', 'abort': 'img'
};
function isEventSupported( eventName, element ) {
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
var isSupported = eventName in element;
if ( !isSupported ) {
if ( !element.setAttribute ) {
element = document.createElement('div');
}
if ( element.setAttribute && element.removeAttribute ) {
element.setAttribute(eventName, '');
isSupported = is(element[eventName], 'function');
if ( !is(element[eventName], 'undefined') ) {
element[eventName] = undefined;
}
element.removeAttribute(eventName);
}
}
element = null;
return isSupported;
}
return isEventSupported;
})();
isEventSupported('click', document.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment