Commit
isEventSupported
, to make example (and mechanis…
…m of this inference) easier to understand. 2) Change `isEventSupported` to use generic element (DIV), when supplied object doesn't implement `Node` interface (and so has no `setAttribute`). Something like `isEventSupported('hashchange', window)` now internally tries `<DIV>.setAttribute('hashchange', '')`, since `window` is one of `setAttribute`-less objects. This change fixes some of the false positives from previous verion. 3) Add few more HTML5, as well as proprietary "pageshow" and "pagehide" events to test page.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,45 @@ | |
*/ | ||
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; | ||
|
||
// 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); | ||
} | ||
} | ||
|
||
This comment has been minimized.
Sorry, something went wrong. |
||
element = null; | ||
return isSupported; | ||
} | ||
return isEventSupported; | ||
})(); | ||
This comment has been minimized.
Sorry, something went wrong.
ibolmo
|
||
|
||
var isEventSupportedWithCache = (function(){ | ||
|
||
var TAGNAMES = { | ||
'select':'input','change':'input', | ||
'submit':'form','reset':'form', | ||
|
@@ -40,15 +79,21 @@ var isEventSupported = (function(){ | |
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those | ||
var isSupported = (eventName in element); | ||
|
||
if (!isSupported && element.setAttribute && element.removeAttribute) { | ||
element.setAttribute(eventName, 'return;'); | ||
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; | ||
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.removeAttribute(eventName); | ||
} | ||
|
||
element = null; | ||
|
3 comments
on commit 6320eda
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in this commit, but should the declared function inside of isEventSupportedWithCache should be named isEventSupportedWithCache. It's currently isEventSupported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've consolidated both methods into one. Since the second only returns the cache iff the cache is populated. Which means that the isEventSupported is called. Moreover, the setting of the cache is only done if the lookup failed.
Was there a reason you wanted to split the cache vs non-cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibolmo
I actually wanted to delete cached version altogether, but copied and renamed it to another method for now. I want to keep this particular method as an example of this technique, without spicing it up with memoization or any other optimizations.
isEventSupportedWithCache
is not used anywhere on a test page, but might still be useful for demonstration purposes.
Feel free to fork it and tweak as desired ;)
Is style the reason why the method was not inlined?