0

Attached is a screenshot of the developer console. I see a syntax error in a closure definition.

JavaScript syntax error

I've a JavaScript file, which has the following function. I'm loading the JavaScript file from another function and calling sampleFunction() from it.

var sampleFuntion=function(obj){
    //Statement here;
};

But Firefox shows a syntax error for the first line, which is the function declaration/definition.

I'm using Mozilla Firefox 29.0.1 on Windows 7.

What is the actual error in the code?

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
Rupam Datta
  • 1,849
  • 1
  • 21
  • 36

3 Answers3

2

As the indicated script line doesn't contain any visual syntactical error, the problem is probably either an invisible illegal character within that line like e.g. a zero-width space. In that case just rewrite your function and ensure you replace everything up to the beginning of the file.

Or the line number is incorrectly displayed within the console and the error is actually somewhere else. To test that simply remove that function from the script and see whether there is still a syntax error displayed at line one afterwards. If so, you may remove other parts of your script and step by step check when the error disappears.

Another way to check whether it's a bug within Firefox is to confirm that error with another browser, i.e. see if it is also displayed at the same line in the console of other DevTools. If it isn't, you should create a new Firefox profile to try out whether the error message actually comes from within your code. If the new profile doesn't display the error, chances are high that the error is caused by some add-on or plugin.

One little note regarding this:

Firebug most of the time also provides information about the column where the syntax error occurred and indicates the exact place:

Syntax error with column displayed within Firebug

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
  • +1 for the answer. I've re-checked my code following your answer. There is not illegal space or character. I'm debugging and if I come across anything I'll post. – Rupam Datta May 29 '14 at 05:51
2

This is caused due to the difference in "Content-Type" / "MIME Type" (or Media Type) [ Content & Mime Types, both are not exactly the same ] returned in the Response Headers of the HTTP Request and the type of Content that is being loaded from the files(in this case, its "JavaScript" content). If MIME Type isn't mentioned, the Firefox is, by default assuming the "Content-Type" as "application/xml", which is not the right type.

Default content type can be verified using:

yourXHRObject.getResponseHeader('Content-Type');

Hence, Firefox Dev Tool throws above errors (Yeah, FF doesn't give any clue!!!)

Solution:

yourXHRObject.overrideMimeType('text/javascript'); or yourXHRObject.overrideMimeType('application/javascript')

as discussed in this GitHub thread, before sending the request. The errors disappears even if "text/plain" is sent to overrideMimeType(). But, technically, one of the above are more apt than "text/plain".

-1

The only instance of "not well-formed" that I can find within the Firefox codebase is from within the XML parser... Please check that you're including the script correctly.

It's also possible that "line 1" refers to e.g. an eval or innerHTML set or similar done from within constant.js.

Simon Lindholm
  • 2,266
  • 1
  • 21
  • 12
  • The "not well-formed" error has got to do with the JSON object that I'm passing and I know the answer. My problem is with the second error I mean the syntax error in filecomponent.js. Thanks. – Rupam Datta May 28 '14 at 15:24