Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE 11 DispatchEvent

Tags:

javascript

We have a datepicker (in JavaScript) that has a section for checking IE 8 and older and other modern browsers.

if(-1 != navigator.userAgent.indexOf("MSIE")){
obj_caller.target.fireEvent("onchange");
}
else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}

It's working fine in Chrome, Firefox, IE8 and below but is failing in IE 11. What I need is a way to get the else part working in IE 11. I just cannot figure out what is failing and how to fix it.

Thanks.

like image 373
JohnnyCage Avatar asked Dec 24 '14 23:12

JohnnyCage


People also ask

What is the use of dispatchevent?

dispatchEvent () is the last step of the create-init-dispatch process, which is used for dispatching events into the implementation's event model. The event can be created using Event constructor . See also the Event object reference. See Creating and triggering events.

Can I cancel an ieventtarget dispatch event?

Document Object Model (DOM) Level 3 Events Specification, Section 4.3 Events that the IEventTarget::dispatchEvent method dispatches are subject to the same capturing and bubbling behavior as events that the browser dispatches. You cannot cancel some events. For more information, see the documentation for the event.

How do I dispatch an internal event?

You cannot dispatch an internal event. Document Object Model (DOM) Level 3 Events Specification, Section 4.3 Events that the IEventTarget::dispatchEvent method dispatches are subject to the same capturing and bubbling behavior as events that the browser dispatches. You cannot cancel some events.

Can I dispatch events from an HTML5 document?

For more information, see the documentation for the event. In general, events can only be dispatched from elements that are part of the document object model, per the HTML5 specification.


1 Answers

Your problem is that fireEvent shouldn't be used in newer IE versions. Support for dispatchEvent was added in IE9. http://help.dottoro.com/ljrinokx.php

if(document.createEventObject) {
    obj_caller.target.fireEvent("onchange");
} else {
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("change", false, true);
    obj_caller.target.dispatchEvent(evt);
}
like image 159
Ashley Lee Avatar answered Oct 07 '22 23:10

Ashley Lee