"item" is a Reserved Word in Internet Explorer 6 and 7

Posted on 26 January 2009 by Johannes Fahrenkrug. Tags: JavaScript Programming

Arg! I know every web developer hates the IE. But sometimes the reasons why we hate it so much become so painfully obvious again. Like today. I've been working on an OpenSocial application and while testing it in IE, I got this strange message:

"Object doesn't support this action"

Then a line number, no script name and just an unselectable, unclickable, uncopiable (I know, I just made all those words up) URL. How helpful.

So first I needed some sort of JS debugging in IE. I went with Companion.JS. It's pretty useful, but of course no comparison with Firebug. I first tried using Firebug Lite, which is really cool, but it didn't work on MySpace (where I had to test my OpenSocial app). By the way, the Microsoft Script Debugger - which is needed by Companion.JS - doesn't install on Vista, of course. It did work on Windows XP, though.

So equipped with a JavaScript debugger, I investigated the error again and actually got a filename and a line number that was useful. This was the offending code:

for (var i = 0; i < json.items.length; i++) {
item = json.items[i]; // more code...
}
It turns out that "item" is a reserved keyword in Internet Explorer. Entering
javascript:alert(item);
in the IE address bar greets you with an [object] alert. This blog post by Sandro confirms this. So all I had to do was rename all instances of "item" to something else.

Comments

Johannes Fahrenkrug said...

Hi Tammo,

Thank you for your comment! You're absolutely right. Not using var was a Bad Thing (tm) :)
I was wondering if IE would choke on it even with var, but alas! it doesn't.
javascript:function itemtest(){var item="test"; alert(item);};itemtest();
presents an ugly "test" alert box. Who would have thought? ;-)

January 28, 2009 02:24 PM

Tammo Freese said...

Hello Johannes, just use var item instead of item. Without var, you set the value globally, which is not what you want to do (at least I hope so :))

January 28, 2009 12:40 PM

Comments

Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus