2007-12-21

Why I hate IE

I simply can't understand why is it that this don't work...
var Utils = new Object();
Utils.byId = function( id ) {
if( document.all ) {
return document.all[ id ];
}
return document.getElementById( id );
}
It is just a utility function which returns me an element by its id attribute. Nothing special. But then... There is a line saying:
panorama = Utils.byId( "panorama" );
It works. Well, it worked in Firefox and Opera. But in IE, there was an error saying something like: object doesn't have that property... I can't remember now (I'm on my Ubuntu linux PC now). It took me half an hour of experiments before I found the error. The error was the word var. The line:
var panorama = Utils.byId( "panorama" );
don't work without the "var".

When I was studying Javascript, in the manual it was said that if I use var for declaring variables -- they were local variables. Without var they are global variables. Since panorama in my javascript is a global variable, I decided to delete the "var". But IE didn't like it.

Why?

2 comments:

  1. This is because of IE's buggy Global Namespace pollution.

    Since "panorama" was an ID, IE added it as a global object called panorama. Thus when you try to use "var" you are trying to re-declare a circular reference.

    See/track the bug here:
    http://webbugtrack.blogspot.com/2007/09/bug-162-global-namespace-pollution-in.html

    ReplyDelete
  2. Now I understand, thanx Bradley!

    BTW, nice blog! Why don't you allow comments there?

    ReplyDelete