I have written the below code and it is working in all modern browsers except in <IE9
<script>
function eventH(event) {
alert(this);
}
</script>
<img src="h300.png" onload="eventH(arguments[0]);"/>
It fires event handler, but event is undefined and this value is parent object. Why?
Does IE handle this differently or I am wrong somewhere in understanding this?
Internet Explorer 8 and below do not implement the W3C event model. For IE, what you need might instead be part of
window.event.QuirksMode gives a good set of examples of how to write event-handling code compatible with both modern browsers and old IE versions.
Also note that when you call a JavaScript function without using dot notation, the global window object is
this(except in ES5 strict mode, in which accessingthisis not allowed in this case). Instead, you can passthisto your function as an argument.However, it may be better to just attach your event handler using .addEventListener() (W3C model) or .attachEvent() (Microsoft model) instead of specifying it in the HTML. Then,
thiswill work as expected.