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().

Join the conversation

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!

I love you, dearly !!!

I've been lugging this problem along now for, oh, 3 weeks, and I kept on failing. I was using stickmanlabs' accordion, and it just wouldn't work with Internexcrement Explorer.

Your little "could be an extra comma" issue solved my problems. And just in the nick of time, I'm showing this thing to my client tomorrow morning :)

Thanks mate!

I wonder why Microsoft

I wonder why Microsoft doesn't make a real effort to improve its crappy web browser. Is it that they have more important things on their agenda? Isn't the Web important enough?

Wow thanks. I'm lucky this

Wow thanks. I'm lucky this was the first thing in Google that came up. I could've beat my head against the wall for hours with this.

I just spent a good deal of

I just spent a good deal of today dealing with jquery IE quirks...i really wish that browser would just die! Even Chrome, a mere few months old, didn't have any trouble. Just IE.

Thanks for this, I had

Thanks for this, I had written a function with "class" as parameter and encountered the same issue. You've saved me a headache.

hello senior DEVELOPERS! i

hello senior DEVELOPERS!
i just want to find out the reason and remedy behind IE giving d erre, grid_gb is undefined as working fine in FF.

Thanks! saved me some time!

Thanks! saved me some time! :)

Dont use length either, I had

Dont use length either, I had a text input with name='length' id='length' and it stopped all my javascript validation working on the form.

Are you people really that

Are you people really that oblivious to the need for a library such as jQuery to reserve the variable 'class' for its own uses?

If it's used for something already, don't use it again.

saved my life..

saved my life..

I owe you a beer! Thanks for

I owe you a beer!

Thanks for posting this, saved me a ton of time.

Rich C.

thank you very much. You save

thank you very much. You save my time :)

well you must admit your

well you must admit your naming convention is silly.

Thanks, u'r suggestion solved

Thanks,

u'r suggestion solved one of the issue in my application.

in my case this is the line which was raising the issue

Hi and thank you so

Hi and thank you so much.
really you save my time and my sources.

Thanks so much, great post.

Thanks so much, great post. Still saving headaches 2 years later, good work!

And IE sucks. Boo down with IE

Thank you so much! I spent 8

Thank you so much! I spent 8 hours yesterday, writing and rewriting an ajax script because I thought IE was having trouble with jQuery's live() function. I could not figure it out and I was a day behind. Then I came to your site by luck and did a quick search for "class" in my script. Sure enough, as soon as I renamed the local variable, everything worked peachy!

You just saved me a ton of time! Thank you so much for posting your experience so we can all learn from it. I understand that maybe I should have thought of a better naming convention for my variable...but we all slip up from time to time, thanks again!

Sincerely,
Mike

You rock, you saved me some

You rock, you saved me some time.

Thanks a lot for this usefull

Thanks a lot for this usefull post.

IE needs to die... Thanks for

IE needs to die... Thanks for the save!

OMG thank you! I was trying

OMG thank you! I was trying to go through each line of code commenting out bits here and there and nothing was standing out as a potential issue. It probably would have taken me several more hours to figure this out, but luckily I decided to google it instead. Stupid IE...

Thanks again :)

Although you wrote this 3

Although you wrote this 3 years ago, it actually saved me alotta time, THANX A MILLION!!!!!!

Dont be a rude, if its in a

Dont be a rude, if its in a custom object than it wouldnt cause an isssue IE is just dumb and reserves it globally.

var o = {};
o.class = 'hello';

class != o.class;

The only issue would be if o was an DomElement, which its not.

This totally helped me out

This totally helped me out thanks.

WOOOW!!! thank you so much! I

WOOOW!!! thank you so much!
I had exactly the same problem! Have been struggling with this for hours!

Thanks, you saved me from a

Thanks, you saved me from a terrible headache.

Thank you for that, I truly

Thank you for that, I truly needed that info. You only made me waste one cup of coffee :)

thanks a lot there.. you just

thanks a lot there.. you just save me a headache.. i was having the same problem on IE 8.. while it works fine on other browser..

Thank you for the post. I had

Thank you for the post. I had the same problem using the keyword 'data'.

You are my hero. :) Worked

You are my hero. :) Worked like a charm and probably saved me hours worth of work.

THANK YOU!!

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment