简体   繁体   中英

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)

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 .

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

如果要在JSON中返回对ajax请求的响应,请确保对Content-Type使用'application/json'

也许HTTP内容类型不是text/htmlapplication/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

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

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.

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

如果是Rails应用程序,请检查format.js是否在response_to块中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM