Let op: Tweakers stopt per 2023 met Tweakblogs. In dit artikel leggen we uit waarom we hiervoor hebben gekozen.

XMLHttpRequest and the scope of events

By crisp on zaterdag 6 januari 2007 22:40
Category: Javascript, Views: 5.227

Sounds like the title of an adventure story right? Well, in a way it is...

I was having an discussion the other day in the comments of one of Microsoft's IE-team's blogpost in which they are again trying to sell bad coding practices as 'performance recommendations' instead of just admitting that IE's JScript and DOM-implemention are just broken. 'Dao' made a remark about eventhandlers and that the 'this' keyword should point to the object instance of javascript extension objects when used within an eventhandler. I pointed out that you cannot rely on that because there is no specification that says that events should be executed in the object instance's scope for these kind of objects and that it totally depends on the object's implementation.

As an example I used the XMLHttpRequest object on which in Firefox the 'this' keyword when used in the readystatechange eventhandler points to the function object that handles the event instead of to the XMLHttpRequest object instance.
I rethought that comment later and thought I'd try it in other browsers as well. I used a small script like this:
var foo = new XMLHttpRequest();
foo.open('GET', 'http://localhost/foo.html', true);
foo.onreadystatechange = function() { alert(this); }

And here are the results:
IE: 'this' refers to the global namespace (window object)
Firefox: 'this' refers to the anonymous function object
Opera: 'this' refers to the XMLHttpRequest object instance ('foo')

3 browsers, 3 different implementations...

Fortunately W3C is trying to Formalize XMLHttpRequest by basically reverse-engineering Microsoft's own propriety implementation and adding some sugar. This working draft mentions that the readystatechange event MUST implement the DOM level 3 Event interface. Now that's interesting, so I checked which browsers actually (seem to) do this:

IE: nope (not even their own whacky event-model)
Firefox: addEventListener seems to be a valid method of the object instance but trying to use it will result in a NS_ERROR_ILLEGAL_VALUE error for the isCapture argument (that argument also doesn't make much sense for these kind of events)
Opera: nope

It does make some sense to implement the DOM Event interface on javascript extension objects although there seems to be some discussion about that. What really does make sense is to try to formalize the scope in which such events should be executed, and not only for the XMLHttpRequest object but for all javascript extensions objects that make use of some kind of events.

It is funny to see how this working draft already makes an assumption about the scope being the object instance as you can tell by the provided example - an example which at this point will not work in IE or Firefox :P

Volgende: Broken parsers; the importance of BASE 01-'07 Broken parsers; the importance of BASE
Volgende: Internet Explorer and cacheing: beware of the Vary 12-'06 Internet Explorer and cacheing: beware of the Vary


Comments are closed