简体   繁体   中英

Google Chrome Extension Javascript Unexpected Token Illegal

This may seem familiar, but I can't find anything about it...sure I've found stuff about "Unexpected Token Illegal," but these circumstances don't relate enough for those solutions to make sense.

I have my background page , like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Background Page</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="background.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

</body>
</html>

A small piece of w3-compliant markup, yet this is where the error "Unexpected Token ILLEGAL" occurs, on line 1. If I add a blank line at the top, it still throws an error at line one.

In my Javascript, I call three initialization functions:

localData.init();
localRead.init();
getTitles();

If I comment all three of these functions (no fewer), the error goes away. I've triple checked the rest of my code, and can see nothing wrong, syntactically or logically.

I am completely stumped...I've been programming in Javascript for years, and yet it still finds oddities to throw at me.

I know this is months old, but I just had the exact same issue with an extension, and it was killing me as I struggled to fix it as quickly as possible. I tried messing with the encoding, replacing all the linebreaks, etc. until just running window.location.reload() in the Chrome console. This time, instead of returning a reference to the beginning of the HTML file, the debugger pointed to a line in an external JavaScript file. Weird!

The bottom line was that in an old version of the extension, I had a text value stored without using JSON.stringify . So when attempting to JSON.parse that value, everything failed. Here's what it looks like in the console:

> localStorage.good = JSON.stringify("good");
  localStorage.bad = "bad";
  console.log(JSON.parse(localStorage.good));
  console.log(JSON.parse(localStorage.bad));
> good
> SyntaxError: Unexpected token ILLEGAL

If that occurs while a page is loading, the page won't appear to render past the <!doctype> declaration. In reality, it does, but it gets hung up. Eeevil.

Just in case anybody else stumbles across this question, I have another example of the error Uncaught SyntaxError: Unexpected token ILLEGAL . For me it was appearing in prototype.js, as Prototype attempted to evaluate scripts in my markup after an AJAX call had completed.

The issue in my case was that I had a simple block:

<script>
</script>

It appeared empty because it was dynamically generated, and I didn't have much logic in place to handle the situation where the block would be empty.

For some reason the Chrome parser gets hung up on a script block with only a \\n newline character in it.

I often get this error when I've included a file that does not exist. Have you checked that jquery.js and background.js are indeed present?

我弄清楚它是什么:我使用的是localStorage功能,最初使用更新功能对其进行初始化,尽管一旦更新功能要求事先进行了定义,就会发生此错误……最后,看来我正在将null传递给JSON.parse,但由于某种原因失败了。

如果您在上面运行代码, JSLint怎么说?

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