I am surprised that until today, Internet Explorer 8 is not supporting DOM 2 Events. This can be tested using method that is described on DOM 2 Events like addEventListener.
I try to make a very simple sample like in Javascript:
<script language="javascript" type="text/javascript">
var btn = document.getElementById('btnShow');
btn.addEventListener('click', function() {
alert('DOM 2 Alert A');
}, false);
btn.addEventListener('click', function() {
alert('DOM 2 Alert B');
}, false);
</script>
Code above in Firefox 3.0.8 and Google Chrome 1.0 work perfectly. It will register functions that will executed when the button click. It will display dialog box twice. I try in Internet Explorer 7 and 8. It says that object doesn;t have property or methods.
Internet Explorer have methods that similar to DOM 2 addEventListener; that is called attachEvent. This methods work the same as addEventListener by registering many functions that is executed when event triggers. The only different is attachEvent register the functions in LIFO while addEventListener register in FIFO.
Below is the simple complete html + javascript:
<script language="javascript" type="text/javascript">
var btn = document.getElementById('btnShow');
var browserName = navigator.appName;
if (browserName == 'Microsoft Internet Explorer') {
btn.attachEvent('onclick', function() {
alert('NON DOM 2 Alert A');
});
btn.attachEvent('onclick', function() {
alert('NON DOM 2 Alert B');
}
);
}
else {
btn.addEventListener('click', function() {
alert('DOM 2 Alert A');
}, false);
btn.addEventListener('click', function() {
alert('DOM 2 Alert B');
}, false);
}
</script>
In the Internet Explorer, dialog with NON DOM 2 Alert B will appear first and then NON DOM 2 Alert A because attachEvent is registering in LIFO
In the Firefox and Google Chrome, dialog with DOM 2 Alert A will appear first then DOM 2 Alert B because attachEvent is registering in FIFO
0 comments:
Post a Comment