IE9 Quirks mode doesn't pass event parameter to event handler

冷暖自知 提交于 2019-12-25 00:27:43

问题


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
  }
}


来源:https://stackoverflow.com/questions/21039060/ie9-quirks-mode-doesnt-pass-event-parameter-to-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!