JS error 'redeclaration' of var when it's first line in program?

I got this error with the following code:

var window;

I had added this declaration to workaround a node error when using the following code:

if (!window) {
  //node-specific stuff
}

Without the above declaration, node wold complain. In the end, I chose to copy/paste rather than try to share the exact same file between node and browser implementations.


EDIT: Fixed it. For me, anyway. I got this error before the redeclaration error:

HTML1113: Document mode restart from Quirks to IE9 Standards 

This suggests that IE finds what it thinks is an error, so loads the page again in Quirks mode. Loading the page twice makes it think everything is declared twice. So the solution is to find what IE didn't like.

First I ran the page through the online HTML validator. Next I ran my javascript through jsLint. After all that, IE9 seemed happy. And as a bonus I have better quality code. I hope.


I had a similar problem with the same error, but my first line of code was an alert(0); which I had inserted to make sure the script was being loaded! Interestingly, the script was loaded according to IE9's developer tools, but the first line was never executed and the error pointed to this alert(0); as the redeclaration. I even inserted lines and spaces before it and the line and character number changed accordingly. However, this (obviously) wasn't the thing being redeclared because it's not even a declaration, let alone a redeclaration!

I deleted chunks from the end of the script until it executed the alert(0); (indicating that the script had loaded and been parsed successfully), and I discovered that the offending line was:

var screen;

Turns out IE9 already has a window.screen with which this declaration collided, and renaming my screen to eScreen fixed the problem.

So my answer is: don't trust IE9's indication of where the redeclaration is!

(It's also worth noting that the script ran fine in its original form on IE7, IE8 and IE10, just not on IE9.)