Bug Demo

This page demonstrates a really weird bug in Internet Explorer 7. The keyword this is always supposed to point the parent object of a function. In IE7 this is not true for functions belonging to the window object!

Only three of the above buttons will produce the correct output in IE7. All the above example functions can be called as members of the window object, but only f3 and f4 seem to execute in the correct scope. This also affects exception handling: An exception thrown by f1 or f2 cannot be caught by the calling code, but somehow vanishes into the void.

This problem is very annoying if you want to create a global function from within another function:


function func() {
	
	window.globalFunc = function() {
		alert(this === window);
	}
	
}

As a workaround you could use implicit globals. However this would cause Mozilla zu issue Javascript warnings in strict mode.


function func() {
	
	globalFunc = function() {
		alert(this === window);
	}
	
}

Luckily, predefined event-handlers are not affected, so window.onload still works as expected.