Last active
April 19, 2024 15:39
-
-
Save yue1123/a4c73e45eda8e180f033875e3d43fb85 to your computer and use it in GitHub Desktop.
Determining whether a browser supports an event
This file contains hidden or 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
| 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; | |
| })(); |
This file contains hidden or 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
| isEventSupported('click', document.body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment