17

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)

cgp
41.4k13 gold badges107 silver badges131 bronze badges
asked Dec 26, 2008 at 5:58
1
  • 1
    Can 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. Commented Feb 2, 2010 at 1:08

10 Answers 10

14

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.

cdeszaq
31.4k27 gold badges123 silver badges176 bronze badges
answered Feb 26, 2010 at 14:27
Sign up to request clarification or add additional context in comments.

2 Comments

Are you able to supply an example of the code edited with the force header change?
I think you are meaning this one header('Content-Type: text/html; charset=utf-8');
2

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.

Nakilon
35.2k16 gold badges112 silver badges149 bronze badges
answered Jun 2, 2010 at 23:31

Comments

1

Maybe the HTTP content type is not text/html or application/xhtml+xml?

answered Dec 26, 2008 at 7:09

1 Comment

I would I determine the HTTP content type?
1

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

answered Dec 26, 2008 at 7:09

Comments

1

Try switching the order of your css and js declarations. Worked for me.

Jim Ferrans
31.1k12 gold badges59 silver badges83 bronze badges
answered Feb 8, 2010 at 6:01

Comments

1

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 '&amp;' - once this was done the Chrome error went away.

Steve

answered Aug 15, 2011 at 5:54

1 Comment

To extend this, data from a db query with '&'s in it will also cause this error...
1

If you are returning a response to an ajax request in JSON, make sure you use 'application/json' for the Content-Type.

Jérôme Verstrynge
59.9k97 gold badges297 silver badges469 bronze badges
answered Jun 6, 2010 at 20:10

Comments

0

If it's a Rails App check that the format.js is in the response_to block.

answered Nov 29, 2010 at 14:53

Comments

0

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.

Prasad Jadhav
5,27416 gold badges65 silver badges82 bronze badges
answered Apr 23, 2010 at 21:56

Comments

0

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! :)

answered Jan 31, 2015 at 0:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.