Internet Explorer Javascript Errors
July 21st, 2006
Evolution and Geneone have been plagued by some Javascript errors in Internet Explorer over the last few weeks. To be honest, the number of people who play Evolution or visit my blog using IE are so low that no one has actually bothered complaining.
These errors included Object expected and "Expected identifier, string or number". I’ve never tried to actually fix these errors before because IE’s script debugging really really sucks.
Windows Scripting Debugger can be a useful tool for debugging Javascript but be warned that it is very very unstable and severely affects the stability of your computer.
Expected identifier, string or number
This was an annoying little bug caused in a class because of a comma at the end. In Javascript you have lists like:
var list = { opacity: 0.5, height: 0.5}
Similar syntax is used to group functions:
var MyFuncs = {bleh : function() {},bleh2 : function() {},}
The above code should work fine in real web browsers such as Firefox. IE will give a screwed up error such as "Expected identifier, string or number". Just remove the comma after the last function and the error be gone.
Object Expected
If you document.getElementsById(’nonexistantitem’) in Firefox and try to retrieve an attribute such as previousSibling, Firefox won’t complain. IE will. This was causing issues as I had a little bit of Javascript which searched for the "Entry Title" box above the "Message" box in the Object Creation forms. On pages where the Entry Title field weren’t shown, IE would freak out as the previousSibling would return False.
I should thank neonDragon for his assistance in resolving these issues
- Browsers , Javascript , Web Development
- Comments(26)
The actual syntax is:
var list = {
a: function() {
},
b: "some value"
};
(note the semicolon at the end)
Also, it’s good practice to actually error check your code.. e.g.:
var elm = document.getElementById(’whatev’);
if(elm) { … }
P.S.You should check out JSLint, it will help you debug your javascript.
Ah cool, thanks for the information. The problem was I was trying to look for the element which came 2 places before a specific element. So I had code like:
document.getElementById(’magnets’).previousSibling.previousSibling.previousSibling.previousSibling.
It was possible that .previousSibling.previousSibling didn’t exist so I had to check for that before calling previousSibling again twice.
Thanks for the info about JSLint, should come in really handy!
awesome. saved my butt when all was lost… these IE error messages just never help.
Thank you for posting this. I have a JSON object that is >60K and it would have taken me forever to find that I have an extra comma at the end. You just saved me a ton of time.
You went probably way too far - too much bother to fix that as interestingly enough its mostly M$ own sites that also trigger that error message (my favourite being their beta site). Don’t know, dear Microsofties but you happen to test your own web pages with firefox? It WOULD be clever to do so, I’d grant you that
Thank you, thank you, thank you! I just spent half an hour or more wrestling with the "Expected identifier, string or number" error!
thanks for this article, helped me with my problem!
Thanks very much for the article.
I almost crushed my head figuring out what is wrong with my code. The most nasty thing is that Explorer has allowed code to be executed partly, which really disturbs and hides the problem.
Know this was prob an old post but I just wanted to thank you for this stumbled upon your blog via google. Thanks a mill spent way too long trying to figure that one out! IHATE IE!!!
But thanks
Thank you! Your article helped me fix an issue developing some Javascript in IE.
Thanks a million for this post. You are a lifesaver !
Thank you, thank you.
You saved me plenty of time and hair-pulling.
Bloody little comma, who would’ve known.
And the useless error message doesn’t help.
Thanks again.
I’d like to add another Thank You! Been fighting a stupid IE error for hours, thanks to this page I tracked it down to a trailing comma in an AJAX data array, about a thousand lines away from where IE was bitching about, in a totally different function!
— hugh
I am using similar kind of editor as this is used for comments typing. I am adding this javascript editor in my website. The problem is if I put some comma like this ( ‘ ) and save in database. Again when i click to edit these text then it won’t show the textarea with value getting from database. Without comma ‘ it works ok.
Is there any solution or fixes in javascript. thanks
Ah, I love the internet. You just solved a very perplexing problem I was having in IE. Argh, ye blasted comma! Anyway, cheers –
[...] down the problem causing obscure “Expected identifier, string or number” errors. It turns out that it was caused by having a comma after the last element of a list, e.g. 1 2 3 4 5 6 7 var list [...]
hi
I just want to say thank you so much for the information
i was fighting this bug for about 1 - 2 hours until i found the f***ing comma after searching at the almost the end of the function , now everything is work well
thanks again
2 bugs down… 369,532 to go… If only the rest of the world would use Firefox, eh?
Gee, this posting just solved a problem I was having. I had no clue that this comma I accidently left at the end of an array would break IE. Thanks!
[...] two years ago, I experienced an persistent error in my Javascript development in one of my scripts. It was the case of the “Expected [...]
OH Just Thank you so much for your post….
You have saved me tons of time.
“Expected identifier, string or number” error comes if we have null has key in some object. E.g.: {null:”TEST”}
You can try using Other Alternate Browsers, if problem is really with your Internet Browser or the system.
I use http://www.goodbyeie6.org.ua/ and forgot about IE6. Plus my users downloads new browsers.
Thank you for posting this. I have spent way too many hours trying to get things to work in IEEEEEEEEE (sorry to scream). I think you hit the nail on the head when you talked about internet explore being hard to debug scripts, they are virtually impossible.