IE9 Quirks mode doesn't pass event parameter t

2019-09-07 04:24发布

问题:

I have a problem with the IE9 quirks mode. I have registered an oncahnge-event to an input element. This works so far, but in IE9 our site goes to quirks mode (it is as it is) and there I have the problem, that the browser doesn't pass the event-Object into my handler method.

document.getElementById("foo").onchange=myChangeMethod;
function myChangeMethod(event){
  //In IE 9 quirks mode "event" is undefined...
  if(event != undefined){
    //do stuff
  }
}

This can be tested in IE10 (i suppose als in IE9) with opening the WebDeveloper Console (F12), Setting BrowserMode to "IE9" and the Document-Mode to (IE5-Quirks). Then the browser behaves the same like setting the native IE9 to "IE 9 Compatibility mode" and Document-Mode to "Quirks".

Is there a possibility to get the event-Object somehow?

Thanks in advance!

回答1:

I suppose the event is global in IE quirks mode, so checking the event parameter and assign window.event if it's undefined would solve the problem:

function myChangeMethod(evt){
  //In IE 9 quirks mode "event" is undefined...
  evt = evt || window.event; //<== HERE
  // additionally in quirks mode evt.target is evt.srcElement, 
  // so if needed you could assign evt.target as:
  var originator = evt.target || evt.srcElement;
  if(evt){
    //do stuff
  }
}