Incorrect handling of the this-keyword in the global scope by IE7

If you define a function as a member of the window object in IE7, the keyword this obviously refers to the wrong object:


window.myFunction = function() {
   return (this === window);
}

A call to myfunction() in IE7 would incorrecty yield false. This problem also affects exception handling. Predefined event-handlers like onload are not affected.

Test page Workaround is included
Reported by: Arnold Konrad.

Explorer 5-6 Windows, Explorer 7 | Reported on 20 March 2007.

This site is no longer maintained. I’m sorry, but it’s just too much work for too little return. You can continue to browse old bug reports, though.




Search reports by browser:

Atom RSS

Comments

(Add your own)

1 Posted by Eric Gerds on 23 March 2007 | Permalink

The 'this' keyword does indeed point to the window object. The problem is that (this==window) gives the wrong result, whereas (window==this) gives the correct result. Definitely a bug, though.

function Z(){
window.a = function(){

alert((this==window) + "\n" + (window==this) + "\n" + this.Z)

}
}

Z();
a()

2 Posted by Arnold Konrad on 24 March 2007 | Permalink

The really annoying thing about this bug is that it affects exception handling:

function Z(){
window.a = function(){

throw new Error("Something went wrong!");

}
}

Z();
try {
a();
} catch(e) {
alert(e.message);
}

The above example won't work in IE.

3 Posted by Eric Gerds on 25 March 2007 | Permalink

Yep, definitely a bug.

Add: "var a = null" in front of your code, then it works for me.

Don't know why though.