Digging into a mysterious failure today at work, I discovered an odd factoid about the way Internet Explorer interprets Javascript.

Here’s a small sample HTML file with a chunk of Javascript:

< html >

< head >

< script >

/* When are functions defined? */

function really () { alert ( "Original" ); }

if ( 0 ) {

alert ( "No" );

function really () { alert ( "Yes, really" ); }

}

really ();

</ script >

</ head >

< body > Really </ body >

</ html >



What alerts will display when you load this page? In Firefox, you’ll see “Original”. In Internet Explorer 6 (and I think 7), you’ll see “Yes, really”. In neither browser will you see “No”.

In IE, the redefinition of really() is interpreted and used even though it is inside a block of code that is not executed. Wonder of wonders...

You can fix it by changing the redefinition clause to:

if ( 0 ) {

alert ( "No" );

var really = function () { alert ( "Yes, really!" ); }

}



Oh, Internet Explorer, how can you be right when you feel so wrong?