Possible Javascript Errors: "Expected Identifier, String, or Number"

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

I looked at my code several times and ran jslint, but didn’t understand anything - does anyone know about the general type of JS problems that lead to this error message?

+71
javascript
Jan 27 '10 at 19:40
source share
19 answers

The cause of this type of error may be an incorrect comma in the definition of an object or array:

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

If it appears on a random line, perhaps this is part of the given object that you are creating dynamically.

+131
Jan 27 '10 at 19:49
source share

Using the word class as a key in a Javascript dictionary can also cause the dangerous error "Expected Identifier, String, or Number" because the class is a reserved keyword on the Internet Explorer.

Bad

 { class : 'overlay'} // ERROR: Expected identifier, string or number 

OK

 {'class': 'overlay'} 

When using a reserved keyword as a key in a Javascript dictionary, enclose the key in quotation marks.

Hope this hint saves you a hell debugging day.

+78
Jan 11 2018-12-12T00:
source share

Actually, I recently got something similar in IE, and this was due to the JavaScript syntax of the β€œerror”. I am talking about the error in quotation marks because it was good everywhere, but in IE. It was in IE6. The problem is related to creating a JSON object and an extra comma like

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

IE6 really dislikes this comma after 3. You may look for something like this, problems with minor syntax.

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

Good luck.

+11
Jan 27 '10 at 19:48
source share

This is the final answer: eliminating a tempting but incorrect answer to help others move to the correct answers.

Debugging might seem to cause a problem. However, the only browser that has a problem is IE, and in IE you can only debug code that was part of the original document. For dynamically added code, the debugger simply shows the body element as the current instruction, and IE claims that the error occurred on a huge line number.

Here is an example webpage 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 gave me the following error:

 Line: 54654408 Error: Object required 
+8
Jan 27 '10 at 19:52
source share

http://closure-compiler.appspot.com/home will select this error with an exact reference to the actual line number in the violating script.

+4
Aug 10 '12 at 6:10
source share

Just saw an error in one of my applications, how wonderful, do not forget to specify the name of all javascript properties that match the keyword.

Found this error after visiting an error in which the object, for example:

 var x = { class: 'myClass', function: 'myFunction'}; 

an error was generated (class and function are keywords) this was fixed by adding quotes

 var x = { 'class': 'myClass', 'function': 'myFunction'}; 

I hope to save you time

+4
Sep 06 '13 at 21:26
source share

As noted earlier, adding an extra comma caused an error.

Also in IE 7.0, the absence of a semicolon at the end of the line caused an error. It works fine in Safari and Chrome (no errors in the console).

+2
Aug 11 '11 at 18:30
source share

IE7 is much less forgiving than newer browsers, especially Chrome. I like to use JSLint to find these errors. He will find these misplaced commas, by the way. You will probably want to activate the option to ignore invalid spaces.

In addition to the misplaced commas in this blog post , someone said:

I was looking for an error that only said "Expected Identifier" only in IE (7). My research led me to this page. After some disappointment, it turned out that the problem was that I used the reserved word as the name of the function ("switch"). The error was unclear, and this indicated an invalid line number.

+2
Aug 13 2018-11-11T00: 00Z
source share

Delete the unnecessary character in the function. you will get a solution.

Refer to this

http://blog.favrik.com/2007/11/29/ie7-error-expected-identifier-string-or-number/

+1
Jan 30 2018-12-12T00:
source share

This error occurs when adding or skipping to remove a comma at the end of an array or in function code. For such an error, the entire code of the web page must be observed.

I got it in a Facebook app while I encoded the Facebook API.

 <div id='fb-root'> <script type='text/javascript' src='http://connect.facebook.net/en_US/all.js'</script> <script type='text/javascript'> window.fbAsyncInit = function() { FB.init({appId:'".$appid."', status: true, cookie: true, xfbml: true}); FB.Canvas.setSize({ width: 800 , height: 860 , }); // ^ extra comma here }; </script> 
+1
Jun 26 '13 at 9:32
source share

It sounds to me like a script that was pulled with src and loaded halfway, resulting in the rest of the sine syntax error not loading.

0
Jan 27 '10 at 19:44
source share

IE7 has problems with arrays of objects

 columns: [ { field: "id", header: "ID" }, { field: "name", header: "Name" , /* this comma was the problem*/ }, ... 
0
Feb 05 '13 at 16:55
source share

Another variation of this error: I had a function called "continue", and since this is a reserved word, it threw this error. I had to rename my "continueClick" function

0
Apr 03 '13 at 19:20
source share

Perhaps you have an object with a method constructor and try calling it.

0
Apr 24 '14 at 13:49
source share

You may encounter this problem when using Knockout JS. If you try to set the class attribute as an example below, it will not be executed:

 <span data-bind="attr: { class: something() }"></span> 

Reset the class line as follows:

 <span data-bind="attr: { 'class': something() }"></span> 

My 2 cents.

0
Jun 11 '15 at 15:22
source share

I also ran into this problem. I found below two solutions. one). As mentioned above, remove the extra comma from the JSON object. 2). Also, I had JSP / HTML. Because of this, he launched the old browser mode, which gave a JS error for the extra comma. When used, it launches the HTML5 browser mode (if supported), and it works fine even with Extra Comma, like any other FF, Chrome, etc. browser.

0
Aug 05 '15 at 22:55
source share

Here is an easy way to debug the problem: script / console echo code. Copy the code from the console into your IDE. Most IDEs perform code error checking and highlight errors. You can see the error almost immediately in your JavaScript / HTML editor.

0
Jan 22 '16 at 2:57
source share

Had the same problem with a different configuration. This was in the definition of the angular factory, but I assume this could happen elsewhere:

 angular.module("myModule").factory("myFactory", function(){ return { myMethod : function() // <--- error showing up here { // method definition } } }); 

The fix is ​​very exotic:

 angular.module("myModule").factory("myFactory", function(){ return { // <--- notice the absence of the return line myMethod : function() { // method definition } } }); 
0
Feb 19 '16 at 9:59
source share

Use IEDeveloper 'and you can fine-tune the error, and the tool even shows a line where there is an error, try and enjoy

-one
Sep 22 '13 at 14:36
source share



All Articles