When using Google Chrome, I receive the following error message:
Error:
Uncaught SyntaxError: Unexpected token <
It occurs directly after my doctype declaration at the top of my HTML page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Any ideas what this JavaScript error message is? It only seems to occur with Google Chrome (works fine in Safari, Firfox and IE)
-
1Can you produce a small reduction? If so please submit a bug report to crbug.com/new and let me know the bug number so I can send it to proper people.Mohamed Mansour– Mohamed Mansour2010年02月02日 01:08:35 +00:00Commented Feb 2, 2010 at 1:08
10 Answers 10
This problem occurred for me when I was using JQuery to load in HTML from an XMLHTTPRequest that was HTML, but the mime type was text/javascript.
So for example, I had some code such as:
jQuery.ajax({
data:'params=here',
success:function(request) {
jQuery('#country_list_container').html(request);
},
type:'get',
url:'/getHtml'
});
jQuery is sending a request to getHtml, and getHtml is returning a simple snippet of HTML, with a mime/type however of "text/javascript". The reason the getHtml action was returning text/javascript is because the accept header of the request from jQuery is:
"Accept:text/javascript, text/html, application/xml, text/xml, */*"
So, all I had to do was force a header of text/html and everything worked perfectly.
The parse error you are seeing is a result of Javascript trying to eval the content.
In my case it was caused by an eval() statement aborting. When I fixed the problem the error message disappeared.
At the present time (June 2010) Chrome Javascript has a buggy eval() statement. eval() will abort without issuing an error message if a variable in the eval() statement has been assigned the reserved word 'delete'. It took me almost a week to uncover this bug because eval() aborts without any warning or error message, and the abort has nasty side-effects.
I was using eval() to achieve this result: obj.delete = somevalue.
This code will cause an eval() abort:
var obj = new Object();
var x = 'delete';
var y = 2;
eval('obj.' + x + ' = y');
This code works:
var obj = new Object();
var x = 'delete';
var y = 2;
eval('obj[\'' + x + '\'] = y');
In summary, don't use eval() to construct obj.delete = somevalue. Use eval() to construct the equivalent statement obj["delete"] = somevalue.
Comments
Maybe the HTTP content type is not text/html or application/xhtml+xml?
I found this Google Groups question.
Some others are experiencing the problem but without resolution.
http://www.google.com/support/forum/p/Chrome/thread?tid=7e9f87870a37e401&hl=en
Comments
Try switching the order of your css and js declarations. Worked for me.
Comments
At the risk of dredging up an old thread (I figure its better to keep all the info in one place), Chrome is still throwing these errors it seems. I got the following while developing a site and tried all kinds of things with the page JS and PHP to get rid of it. The error I got was:
error in event handler for 'undefined': SyntaxError: Unexpected token ILLEGAL
chrome/EventBindings:183
Eventually while checking code validity in FireFox I stumbled across the answer as a warning that there was a '&' in some text that should be converted to '&' - once this was done the Chrome error went away.
Steve
1 Comment
If you are returning a response to an ajax request in JSON, make sure you use 'application/json' for the Content-Type.
Comments
If it's a Rails App check that the format.js is in the response_to block.
Comments
I had this problem when I was trying to set .innerHTML dynamically generated with php (it was generated multi-line).
Replacing new line characters by spaces solved the problem.
Comments
I also encountered this error in Chrome Dev Tools, and my cause was similar to Matthew O'Riordan's except it wasn't the Accept MIME-type but rather the broken AJAX URL.
I had rearranged my folder structure and forgot to update the URL; once I fixed the URL, the Dev Tools JS Error was gone. Hope this helps! :)