Skip to content

Instantly share code, notes, and snippets.

@ibolmo
Created December 18, 2009 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibolmo/259644 to your computer and use it in GitHub Desktop.
Save ibolmo/259644 to your computer and use it in GitHub Desktop.
var isEventSupported = (function(){
var TAGNAMES = {
'select':'input','change':'input',
'submit':'form','reset':'form',
'error':'img','load':'img','abort':'img'
},
cache = {};
return function isEventSupported(eventName, element) {
// only return cached result when no element is given
if (!element && cache[eventName]) {
return cache[eventName];
}
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
var isSupported = (eventName in element);
if (!isSupported) {
// if it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
if (!element.setAttribute) {
element = document.createElement('div');
}
if (element.setAttribute && element.removeAttribute) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] == 'function';
// if property was created, "remove it" (by setting value to `undefined`)
if (typeof element[eventName] != 'undefined') {
element[eventName] = void 0;
}
element.removeAttribute(eventName);
}
}
element = null;
return (cache[eventName] = isSupported);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment