问题
I am creating an event, so use the DOM Event constructor:
new Event(\'change\');
This works fine in modern browsers, however in Internet Explorer 9, 10 & 11, it fails with:
Object doesn\'t support this action
How can I fix Internet Explorer (ideally via a polyfill)? If I can\'t, is there a workaround I can use?
回答1:
There's an IE polyfill for the CustomEvent constructor at MDN. Adding CustomEvent to IE and using that instead works.
(function () {
if ( typeof window.CustomEvent === "function" ) return false; //If not IE
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
回答2:
I think that the best solution to solve your problem and deal with cross-browser event creation is:
function createNewEvent(eventName) {
var event;
if (typeof(Event) === 'function') {
event = new Event(eventName);
} else {
event = document.createEvent('Event');
event.initEvent(eventName, true, true);
}
return event;
}
回答3:
This package does the magic:
https://www.npmjs.com/package/custom-event-polyfill
Include the package and dispatch the event as following:
window.dispatchEvent(new window.CustomEvent('some-event'))
回答4:
If you're just trying to dispatch a simple event like the HTML toggle event, this works in Internet Explorer 11 as well as the other browsers:
let toggle_event = null;
try {
toggle_event = new Event("toggle");
}
catch (error) {
toggle_event = document.createEvent("Event");
let doesnt_bubble = false;
let isnt_cancelable = false;
toggle_event.initEvent("toggle", doesnt_bubble, isnt_cancelable);
}
// disclosure_control is a details element.
disclosure_control.dispatchEvent(toggle_event);
回答5:
the custom-event
npm package worked beautifully for me
https://www.npmjs.com/package/custom-event
var CustomEvent = require('custom-event');
// add an appropriate event listener
target.addEventListener('cat', function(e) { process(e.detail) });
// create and dispatch the event
var event = new CustomEvent('cat', {
detail: {
hazcheeseburger: true
}
});
target.dispatchEvent(event);
回答6:
There's a polyfill service which can patch this and others for you
https://polyfill.io/v3/url-builder/
<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js"></script>
回答7:
I personally use a wrapper function to handle manually created events. The following code will add a static method on all Event
interfaces (all global variables ending in Event
are an Event interface) and allow you to call functions like element.dispatchEvent(MouseEvent.create('click'));
on IE9+.
(function eventCreatorWrapper(eventClassNames){
eventClassNames.forEach(function(eventClassName){
window[eventClassName].createEvent = function(type,bubbles,cancelable){
var evt
try{
evt = new window[eventClassName](type,{
bubbles: bubbles,
cancelable: cancelable
});
} catch (e){
evt = document.createEvent(eventClassName);
evt.initEvent(type,bubbles,cancelable);
} finally {
return evt;
}
}
});
}(function(root){
return Object.getOwnPropertyNames(root).filter(function(propertyName){
return /Event$/.test(propertyName)
});
}(window)));
EDIT: The function to find all Event
interfaces can also be replaced by an array to alter only the Event interfaces you need (['Event', 'MouseEvent', 'KeyboardEvent', 'UIEvent' /*, etc... */]
).
来源:https://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work