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 lifes 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.

Saved some time with this

Saved some time with this article on this exact reserved word issue. The error reporting in IE is useless on this matter.

Thanks and...

Thanks for the "fix". I noticed that vim also shows "id" in the same color as "class". Maybe it's a reserved word also.

I'll throw in another thanks

I've been struggling with javascript/jquery problems in IE for days now and through elimination I was narrowing in on a function call with arguments causing the problems. I had no idea why until I noticed an error icon in IE7 and by translating the error (stupid thing was in norwegian :p) and googling it I ended up here and found:

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

And this extra comma was exactly what was causing all my problems. Seemed like it made IE stop javascript all together.

Thanks alot.

common programming practice

Its actually common programming practice to not use any reserved keywords like class in any language. class has been in javascript for a long time now but FF just doesnt tell you that straight up and ie does. If you use an ide that has support for javascript to code in, it will save lots of headaches down the road because you would see that that name was recognized

Thank you. This saved me

Thank you. This saved me some headache.. Thanks

thx

hey thanks man!
was searching for solution since hours now!

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