From 759793f041c0e258eed8ef596e007ee15ccbefbe Mon Sep 17 00:00:00 2001 From: rmowder Date: Wed, 13 Aug 2014 14:07:11 -0700 Subject: [PATCH] Update EventListener.js try to create the CustomEvent instead of checking for its existence to account for versions or IE (9+) that expose CustomEvent while in standards mode but don't allow it to be used for some reason. --- EventListener.js | 86 ++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/EventListener.js b/EventListener.js index 177ce91..35a51e5 100644 --- a/EventListener.js +++ b/EventListener.js @@ -88,27 +88,31 @@ this.Element && Element.prototype.attachEvent && !Element.prototype.addEventList }); // CustomEvent - Object.defineProperty(Window.prototype, "CustomEvent", { - get: function () { - var self = this; - - return function CustomEvent(type, eventInitDict) { - var event = self.document.createEventObject(), key; - - event.type = type; - for (key in eventInitDict) { - if (key == 'cancelable'){ - event.returnValue = !eventInitDict.cancelable; - } else if (key == 'bubbles'){ - event.cancelBubble = !eventInitDict.bubbles; - } else if (key == 'detail'){ - event.detail = eventInitDict.detail; + try { + new window.CustomEvent('?'); + } catch (e) { + Object.defineProperty(Window.prototype, "CustomEvent", { + get: function () { + var self = this; + + return function CustomEvent(type, eventInitDict) { + var event = self.document.createEventObject(), key; + + event.type = type; + for (key in eventInitDict) { + if (key == 'cancelable'){ + event.returnValue = !eventInitDict.cancelable; + } else if (key == 'bubbles'){ + event.cancelBubble = !eventInitDict.bubbles; + } else if (key == 'detail'){ + event.detail = eventInitDict.detail; + } } - } - return event; - }; - } - }); + return event; + }; + } + }) + }; // ready function ready(event) { @@ -124,22 +128,26 @@ this.Element && Element.prototype.attachEvent && !Element.prototype.addEventList window.addEventListener("load", ready); })(); -!this.CustomEvent && (function() { - // CustomEvent for browsers which don't natively support the Constructor method - window.CustomEvent = function CustomEvent(type, eventInitDict) { - var event; - eventInitDict = eventInitDict || {bubbles: false, cancelable: false, detail: undefined}; - - try { - event = document.createEvent('CustomEvent'); - event.initCustomEvent(type, eventInitDict.bubbles, eventInitDict.cancelable, eventInitDict.detail); - } catch (error) { - // for browsers which don't support CustomEvent at all, we use a regular event instead - event = document.createEvent('Event'); - event.initEvent(type, eventInitDict.bubbles, eventInitDict.cancelable); - event.detail = eventInitDict.detail; - } - - return event; - }; -})(); \ No newline at end of file +try { + new window.CustomEvent('?'); +} catch (e) { + (function() { + // CustomEvent for browsers which don't natively support the Constructor method + window.CustomEvent = function CustomEvent(type, eventInitDict) { + var event; + eventInitDict = eventInitDict || {bubbles: false, cancelable: false, detail: undefined}; + + try { + event = document.createEvent('CustomEvent'); + event.initCustomEvent(type, eventInitDict.bubbles, eventInitDict.cancelable, eventInitDict.detail); + } catch (error) { + // for browsers which don't support CustomEvent at all, we use a regular event instead + event = document.createEvent('Event'); + event.initEvent(type, eventInitDict.bubbles, eventInitDict.cancelable); + event.detail = eventInitDict.detail; + } + + return event; + }; + })(); +}