开发者

Google Chrome Extension Javascript Unexpected Token Illegal

开发者 https://www.devze.com 2023-01-19 22:18 出处:网络
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.

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.


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?


I figured out what it was: I was using the localStorage functionality, and had originally used my update function to initialize it, though once the update function required it to be previously defined, this error occurred...in the end, it seems I was passing null to JSON.parse, which somehow fails.


What does JSLint say if you run your code over it?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号