最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct way to dispatch event in IE 11 - Stack Overflow

programmeradmin7浏览0评论

I have a problem with dispatching event in Internet Explorer 11. Currently, we have:

fireEvent for IE and

createEvent
initEvent
dispatchEvent

idiom for normal browsers.

The prorblem is that neither of these works in IE 11. Nor do the new way - using new Event() / new CustomEvent().

It looks like Microsoft deprecated their proprietary fireEvent (for IE 11) but did not offer support for correct dispatching.

PS. I believe i have read all topics here on SO related to the matter still can't find the working solution

I have a problem with dispatching event in Internet Explorer 11. Currently, we have:

fireEvent for IE and

createEvent
initEvent
dispatchEvent

idiom for normal browsers.

The prorblem is that neither of these works in IE 11. Nor do the new way - using new Event() / new CustomEvent().

It looks like Microsoft deprecated their proprietary fireEvent (for IE 11) but did not offer support for correct dispatching.

PS. I believe i have read all topics here on SO related to the matter still can't find the working solution

Share Improve this question asked Mar 9, 2017 at 22:17 Anatoly YakimchukAnatoly Yakimchuk 3405 silver badges11 bronze badges 3
  • 2 Isn't this the kind of thing that jquery is for? To abstract all of the weirdness and non-working things out so that you can just call .trigger() ....See also this page – Lynn Crumbling Commented Mar 9, 2017 at 22:24
  • @LynnCrumbling Looks promising, i'll try it when i'll be at work – Anatoly Yakimchuk Commented Mar 9, 2017 at 22:35
  • @LynnCrumbling The solution you proposed works flawlessly! Thanks – Anatoly Yakimchuk Commented Mar 10, 2017 at 9:15
Add a ment  | 

2 Answers 2

Reset to default 6

Here's a fully working solution I based on this SO Answer: https://stackoverflow./a/49071358/79677

You can use it like this: SendBrowserAgnosticEvent(myElement, 'change') and it will return the created Event object

Here's the code in JavaScript

/** This allows us to dispatch browser events in old IE and newer browsers */
export var SendBrowserAgnosticEvent = function(elem, eventName) {
    //Needed for IE Support: https://developer.mozilla/en-US/docs/Web/API/EventTarget/dispatchEvent#Browser_Compatibility
    //https://stackoverflow./a/49071358/79677
    var event;
    if (typeof(Event) === 'function') {
        event = new Event(eventName);
    } else {
        event = document.createEvent('Event');
        event.initEvent(eventName, true, true);
    }
    elem.dispatchEvent(event);

    return event;
};

and here it is in TypeScript with full typings

/** This allows us to dispatch browser events in old IE and newer browsers */
export const SendBrowserAgnosticEvent = <T extends HTMLElement | Window>(elem: T, eventName: string): Event => {
    //Needed for IE Support: https://developer.mozilla/en-US/docs/Web/API/EventTarget/dispatchEvent#Browser_Compatibility
    //https://stackoverflow./a/49071358/79677
    let event;
    if (typeof(Event) === 'function') {
        event = new Event(eventName);
    } else {
        event = document.createEvent('Event');
        event.initEvent(eventName, true, true);
    }
    elem.dispatchEvent(event);

    return event;
};

Answering my own question (thanks to the @LynnCrumbling for pointing out):

Browser specific event firing behavior better to replace with unified jQuery call like:

var eventObject = jQuery.Event("change"); // event you need to fire
$(targetObject).trigger(eventObject);
发布评论

评论列表(0)

  1. 暂无评论