Created
December 18, 2009 17:58
-
-
Save ibolmo/259644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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