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

CustomEvent & dispatchEvent vs. Event & dispatchEvent vs. createEvent, initEvent & dispatchEvent vs. createEventObject & fireEvent #2422

Open
jasdeepkhalsa opened this issue Feb 28, 2019 · 2 comments
Assignees

Comments

@jasdeepkhalsa
Copy link

jasdeepkhalsa commented Feb 28, 2019

There are many ways to trigger events, and depending on what the browser actually supports, this can become a complex area.

I'm very surprised Modernizr does not have any feature detection for any event dispatching methods whatsoever, except CustomEvent.

In general, the following are all valid ways to dispatch events:

CustomEvent & dispatchEvent - Limited browsers (https://caniuse.com/#feat=custom-elementsv1)

var event = new CustomEvent('build', { detail: elem.dataset.time });
elem.dispatchEvent(event);

Event & dispatchEvent - Modern browsers (not sure which ones)

var event = new Event('build');
elem.dispatchEvent(event);

createEvent, initEvent & dispatchEvent - >= IE9 & modern browsers (not sure which ones)

var event = document.createEvent('HTMLEvents');
event.initEvent('build', true, false);
elem.dispatchEvent(event);

createEventObject & fireEvent - <= IE8

var event = document.createEventObject();
elem.fireEvent('onbuild', event);

Example cross-browser implementation for dispatching events

Here is an example of an implementation I've written to help trigger events in a cross-browser way - by combining ideas I've seen across the Internet.

However, it's not been tested thoroughly or anything so please take it with a pinch of salt, but it should hopefully demonstrate how complex this problem really is without a clever way to detect features:

function triggerEvent(elem, eventType) {
   const noop = () => {}

   if ('createEvent' in document) {
        // modern browsers, >= IE9
        const evt = document.createEvent('HTMLEvents') || new Event(eventType);
        evt.initEvent ? evt.initEvent(eventType, true, false) : noop();
        elem.dispatchEvent(evt);
        return;
    }
    else if ('fireEvent' in document) {
        // <= IE8
        const evt = document.createEventObject();
        evt.eventType = eventType;
        elem.fireEvent(`on${evt.eventType}`, evt);
        return;
    }
}

I feel like as a developer community we can come up with some clever ways to detect these various browser APIs used for triggering events and add them into Modernizr.

@rejas
Copy link
Member

rejas commented Mar 13, 2019

hi @jhbsk do you want to create a preliminary PR to get the discussion going?

@jasdeepkhalsa
Copy link
Author

Happy to have a stab at it @rejas !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants