What is causing the syntax error I see in Firefox developer console?

三世轮回 提交于 2019-12-11 17:52:25

问题


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

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?


回答1:


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:




回答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".




回答3:


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.



来源:https://stackoverflow.com/questions/23904382/what-is-causing-the-syntax-error-i-see-in-firefox-developer-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!