Javascript Expected Identifier Error On Internet Explorer

A really weird thing was happening with some jQuery code, I was getting this error in both Internet Explorer 6 and 7: Expected Identifier.

I googled a little and found this could happen if you included an extra comma in some expressions but that wasn't my case.

Some coffees later I found the offending code was:

var class = $(this).parent().attr('class');

Yep, class seems to be a reserved word in Internet Explorer, thanks again Microsoft for making web developers lives so difficult.

I just changed the variable name to fix the error, something like this:

var tabClass = $(this).parent().attr('class');

A little later I found another mention of the class problem, hell!, where was this article when I was looking for the fix? It seems you can't set classes with jQuery's attr() method in Internet Explorer either, well, I guess that's why we have addClass() and removeClass().

Trackback URL for this post:

http://ventanazul.com/webzine/trackback/82

Just stopping buy to say I

Just stopping buy to say I hate that peace of crap, too. Guess what? IE is giving me the same headaches right now...

Thanks for the post--saved

Thanks for the post--saved me a few hours of agony. At first I thought it was a document.all or variable scope problem, but turns out you just can't use the word class as a variable.

Thanks

Ha it's always the obvious.
in my case:

js.Return....

Yeah I know dumbass.
Cheers buddy.

Thanks

This post just saved me untold hours of trying to debug IE's cryptic and near useless error reporting. Changed var class to var myClass and now all is well.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.

More information about formatting options