Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE9+ support for CustomEvent while in standards mode. #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 47 additions & 39 deletions EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};
})();
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;
};
})();
}