我在理解如何实现IE CustomEvent Polyfill时遇到了问题。我在其他浏览器中使用CustomEvent但似乎无法使Polyfill工作。
(function () {
// CustomEvent polyfill
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill
if ( typeof window.CustomEvent === "function" ) return false; //If CustomEvent supported return
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;
})();
// CustomEvent supported
// From example: https://www.kirupa.com/html5/custom_events_js.htm
document.body.addEventListener("fontsLoaded", doSomething, false);
function doSomething(e) {
alert("Event is called: " + e.type);
}
var myFontsLoaded = new CustomEvent("fontsLoaded");
document.body.dispatchEvent(myFontsLoaded);