Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created October 31, 2009 09:49
Show Gist options
  • Save fearphage/223011 to your computer and use it in GitHub Desktop.
Save fearphage/223011 to your computer and use it in GitHub Desktop.
isEventSupported test by kangax
/**
* @method isEventSupported
* @param {String} eventName
* @param {HTMLElement} element optional
* @return {Boolean} true if event is supported
*/
var isEventSupported = (function(){
var TAGNAMES = {
'select':'input','change':'input',
'submit':'form','reset':'form',
'error':'img','load':'img','abort':'img'
},
cache = { };
function isEventSupported(eventName, element) {
var canCache = (arguments.length == 1);
// only return cached result when no element is given
if (canCache && (eventName in cache)) {
return cache[eventName];
}
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize"
// `in` "catches" those
var isSupported = (eventName in element);
if (!isSupported && element.setAttribute) {
element.setAttribute(eventName, 'return;');
isSupported = typeof element[eventName] == 'function';
}
element = null;
return canCache ? (cache[eventName] = isSupported) : isSupported;
}
return isEventSupported;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment