Dispatch event issue with IE


#1

Hello,

I want to dispatch the popstate event. The following code works in all browsers but IE:

dispatchEvent(window, makeEvent(popstate))’

In IE this returns with the error “Object does not support this action”.

I found a Javascript solution for this problem, but wonder how to translate that into Reason.

if(typeof(Event) === 'function') {
    var event = new Event('submit');
}else{
    var event = document.createEvent('Event');
    event.initEvent('submit', true, true);
}

window.dispatchEvent(event);

#2

Is there a chance someone could give me a hint on how to migrate this code to Reason?


#3
type evt;

[@bs.val] external eventFunction: 'a = "Event";
[@bs.new] external makeEvent: string => evt = "Event"; 
switch (Js.Types.classify(eventFunction)) {
  | Js.Types.JSUndefined => { failwith("Handle ie8")}
  | _ => makeEvent("submit")
};

Playground link

This will get you most of the way there


#4

That helped. Thank you.

To catch IE11 I had to use Js.Types.JSObject(_) instead of Js.Types.JSUndefined.

I implemented the IE part like this:

[@bs.val] [@bs.scope "document"]
external createEvent : string => Dom.event = "createEvent";

[@bs.send]
external initEvent :
  (Dom.event, [@bs.string] [ | `submit], Js.boolean, Js.boolean) => unit =
  "initEvent";
let ev = createEvent("Event");
initEvent(ev, `submit, Js.true_, Js.true_);