tags:

views:

494

answers:

4

Some users are reporting occasional JS errors on my site. The error message says "Expected identifier, string or number" and the line number is 423725915, which is just an arbitrary number and changes for each report when this occurs. This mostly happens with IE7/ Mozilla 4.0 browsers.

I scanned my code a bunch of times and ran jslint but it didn't pick anything up - anyone know of the general type of JS problems that lead to this error message?

A: 

This sounds to me like a script that was pulled in with src, and loaded just halfway, causing a syntax error sine the remainder is not loaded.

Roland Bouman
+1  A: 

Actually I got something like that on IE recently and it was related to JavaScript syntax "errors". I say error in quotes because it was fine everywhere but on IE. This was under IE6. The problem was related to JSON object creation and an extra comma, such as

{ one:1, two:2, three:3, }

IE6 really doesn't like that comma after 3. You might look for something like that, touchy little syntax formality issues.

Yeah, I thought the multi-million line number in my 25 line JavaScript was interesting too.

Good luck.

cjstehno
+1  A: 

The cause of this types of errors can often be a misplaced comma in a object or array definition:

var obj = {
   id: 23,
   name: "test",  <--
}

If it appears at a random line, maybe it's part of an object defintion you are creating dynamically.

amercader
it was a missing ; in a event assignment. The clue to look at dynamically created objects helped pick this up. jslint didn't find this initially coz it was generated as inline js as part of a html page. Thx!
psychotik
@psychotik: Debugging would have shown you this without making you search for it...
Emtucifor
No it wouldn't. Most modern browsers fix this for you. IE6/7 doesn't. And debugging IE8 in IE7 emulation mode doesn't catch this. (I'm guessing you know that IE7 doesn't have a debugger - using VS with IE7 doesn't catch this either). Try it and then tell me ;)
psychotik
@psychotik: you are absolutely right. My apologies. I am updating my answer.
Emtucifor
A: 

This is a definitive un-answer: eliminating a tempting-but-wrong answer to help others navigate toward correct answers.

It might seem like debugging would highlight the problem. However, the only browser the problem occurs in is IE, and in IE you can only debug code that was part of the original document. For dynamically added code, the debugger just shows the body element as the current instruction, and IE claims the error happened on a huge line number.

Here's a sample web page that will demonstrate this problem in IE:

<html>
<head>
<title>javascript debug test</title>
</head>
<body onload="attachScript();">
<script type="text/javascript">
function attachScript() {
   var s = document.createElement("script");
   s.setAttribute("type", "text/javascript");
   document.body.appendChild(s);
   s.text = "var a = document.getElementById('nonexistent'); alert(a.tagName);"
}
</script>
</body>

This yielded for me the following error:

Line: 54654408
Error: Object required
Emtucifor