for some purpose i want to cache the Window.Event object in a variable and use it later,but MSIE keep telling me that this is ‘unknow’.
just run the code below in IE,you will see what i mean
i just want to ask how can this happen?
did i miss something?
html:
<button id='btn'>Click!!!</button>
JS:
var eventObj = null;
document.getElementById('btn').onclick = function() {
eventObj = window.event;
setTimeout(function() {
alert(typeof eventObj.srcElement);
}, 1000)
}
EIDT 1:
i have search some test done by other ,see the below:
HTML :
<button id='btn1'>Click 1 !!</button>
<button id="btn2">Click 2 !!</button>
JS
var btn1EventObj = null;
document.getElementById('btn1').onclick = function() {
btn1EventObj = window.event;
alert(btn1EventObj.srcElement.id);
}
document.getElementById('btn2').onclick = function() {
alert(btn1EventObj === window.event); // output:false;
alert(btn1EventObj.srcElement === window.event.srcElement); // output: true ;
alert(btn1EventObj.srcElement.id); // output: btn2 ;
}
when the btn1 has been click i assume i cache the ‘event object’ in the btn1EventObj,and then click the btn2:
test:
btn1EventObj === window.event -> false; // there is not only one event object in MSIE
btn1EventObj.srcElement === window.event.srcElement -> true // i can not understand this one the the below.
btn1EventObj.srcElement.id ->btn2
see the fiddle
so all the above tell me that maybe all the event raised in MSIE are all share attributes ,and when the btn2 is clicked,all the previous attribute are overwrite by the later one?
am i kind of right ?
You can’t copy
window.eventout of context of the actual event in IE. In other words: there is no existing event when you assign the handler in the script. If you want to refer to thewindow.event, the handler has to be assigned inline.MSDN:
HTML:
and JS:
eventin MSDN.