Last active
August 29, 2015 14:23
-
-
Save Alex1990/98f993318048f7f4fe65 to your computer and use it in GitHub Desktop.
A cross-browser custom event module.
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
| /** | |
| * A cross-browser custom event module. | |
| */ | |
| var global = window; | |
| var NativeCustomEvent = window.CustomEvent; | |
| function isCustomEvent() { | |
| try { | |
| var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } }); | |
| return p.type === 'cat' && p.detail.foo === 'bar'; | |
| } catch (e) { | |
| } | |
| return false; | |
| } | |
| if (!isCustomEvent()) { | |
| // IE >= 9 | |
| if (typeof document.createEvent === 'function') { | |
| global.CustomEvent = function (type, params) { | |
| var e = document.createEvent('CustomEvent'); | |
| if (params) { | |
| e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail); | |
| } else { | |
| e.initCustomEvent(type, false, false, void 0); | |
| } | |
| return e; | |
| }; | |
| } else { | |
| // IE6-8 | |
| global.CustomEvent = function (type, params) { | |
| var e = document.createEventObject(); | |
| e.type = type; | |
| if (params) { | |
| e.bubbles = Boolean(params.bubbles); | |
| e.cancelable = Boolean(params.cancelable); | |
| e.detail = params.detail; | |
| } else { | |
| e.bubbles = false; | |
| e.cancelable = false; | |
| e.detail = void 0; | |
| } | |
| return e; | |
| }; | |
| } | |
| } | |
| // Trigger a custom event | |
| var triggerInput = function (elem, eventName) { | |
| var event = new CustomEvent(eventName); | |
| if (isCustomEvent() || (typeof document.createEvent === 'function')) { | |
| elem.dispatchEvent(event); | |
| } else if (elem.attachEvent) { | |
| if (!elem[eventName]) { | |
| elem[eventName] = 1; | |
| elem.attachEvent('onpropertychange', function(e) { | |
| if (e.propertyName === eventName) { | |
| elem['on' + eventName] && elem['on' + eventName](evebt); | |
| } | |
| }); | |
| } | |
| elem[eventName]++; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment