Cross Browser compatible CustomEvent
Solution 1
CoustomEvent constructor is not supported in IE11. you can use following pollyfill
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
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;
})();
Copied from MDN
Solution 2
Take a look to the "Can I use" page and what it says about the Custom Events.
http://caniuse.com/#feat=customevent
Specifically, read this quote:
While a window.CustomEvent object exists, it cannot be called as a constructor. Instead of new CustomEvent(...), you must use e = document.createEvent('CustomEvent') and then e.initCustomEvent(...)
![Sarath S Nair](https://i.stack.imgur.com/hxK58.jpg?s=256&g=1)
Sarath S Nair
I am a Front End developer with industry experience building large scale enterprise web applications for fortune 100 companies like Walmart and Oracle. I specialize in UI and have experience building complex user interfaces using the latest web tooling and modern JavaScript frameworks, including React & Redux, ES2015+, Webpack, and NodeJS. I am also familiar with the service-oriented, microservices architecture and interested in full-stack web development. ★ Worked on the complete re-platform of Walmart Canada e-Commerce platform (walmart.ca) using the Walmart electrode framework, which have millions of visits per day. I have contributed to the development of reusable UI components in React.js and implemented internationalization, analytics, and solutions for pushing real-time marketing contents to the live application. ★ Part of the product development team of Oracle Field Service Cloud platform, which manages more than 1 lakh field technicians for a single customer in real-time. I have redesigned the Dashboard and Reports module using KnockoutJS, PHP and Oracle JET framework. Also worked on improving the performance of reports generation by optimizing SQL queries. ★ Worked on the complete redesign and re-platform of Walmart Mexico (www.walmart.com.mx) and Walmart Mexico Sam's Club (www.sams.com.mx) e-Commerce applications using React, AngularJS and Flux. I was also part of the development team of Walmart UK toYou, which is a parcel delivery/return service from ASDA. Skills: HTML, CSS, JavaScript (ES6), ReactJS, Redux, Redux Thunk, Redux Saga, NodeJS, NPM, Yarn, Lerna, Babel, Webpack, Flow, CSS Pre-processors (LESS, SASS), CSS in JS, HapiJS, Unit Testing (Jest, Enzyme, Karma, Mocha) AngularJS, RequireJS, KnockoutJS, Git, JIRA, Browser developer tools, OneOps(Cloud), Jenkins, Agile Take a look at my work or get in touch! http://sarathsnair.me GitHub: https://github.com/sarathsnair Reach out to me on: ✉ [email protected] ✆ +919447780141 ✆ +917012329889
Updated on June 03, 2022Comments
-
Sarath S Nair about 2 years
I need to create a Custom Event which will pass some data to the event listener. I already created one like below
CustomEvent
var event = new CustomEvent('store', { 'detail': obj }); document.getElementById("Widget").dispatchEvent(event);
Listener
document.getElementById("Widget").addEventListener('store', function (e) { console.log(e.detail); document.getElementById("result").innerHTML = e.detail.name+"<br>"+e.detail.address; }, false);
It is working fine in browsers such as Chrome and Firefox but not working in IE11. I am getting an error in IE:
Object doesn't support this action
CustomEvent is not working in IE.
How can I make this cross-browser compatible ? I don't want to use jQuery
trigger()
because theListener
might not be in a page having jQuery -
Sarath S Nair about 8 yearsHow can I send the data { 'detail': obj } using this method ?
-
dlopez about 8 yearsI'm pretty sure that if you save the data in the event objects itself can work. For instance, something like this.
e.myData = { detail: obj};