TeddyR_
TeddyR_

Reputation:

Google Chrome: JavaScript Error Message

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)

Upvotes: 17

Views: 25415

Answers (10)

TzeMan
TzeMan

Reputation: 77

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

Upvotes: 0

BlackMagic
BlackMagic

Reputation: 5698

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.

Upvotes: 2

Matthew O&#39;Riordan
Matthew O&#39;Riordan

Reputation: 8211

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.

Upvotes: 14

dawid
dawid

Reputation: 1

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.

Upvotes: 0

Sacha
Sacha

Reputation: 11

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

Upvotes: 1

Steve
Steve

Reputation: 1459

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

Upvotes: 1

Dinatih
Dinatih

Reputation: 2476

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

Upvotes: 0

Pooch
Pooch

Reputation: 299

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

Upvotes: 1

TeddyR_
TeddyR_

Reputation:

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

Upvotes: 1

orip
orip

Reputation: 75427

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

Upvotes: 1

Related Questions