Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js: "Uncaught SyntaxError: Unexpected token ILLEGAL"?

I've just downloaded D3.js from d3js.org (link to zip file), unzipped it, and referenced it in the following HTML page:

<html> <head> <title>D3 Sandbox</title> <style> </head> <body> <script src="/d3.v3.js"></script> </body> </html> 

But when I load this page, my console (in Chrome) is giving me this error:

Uncaught SyntaxError: Unexpected token ILLEGAL: line 2 

It doesn't like the pi and e symbols at the start of the file. Errrr... what can I do about this? I am serving the file with python's SimpleHTTPServer.

Update: yes I know I can just link to a CDN version, but I would prefer to serve the file locally.

like image 740
Richard Avatar asked Mar 06 '13 00:03

Richard


People also ask

Why do I get unexpected token error?

Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + – var if-else var etc}. Unexpected token is similar to syntax error but more specific. Semicolon(;) in JavaScript plays a vital role while writing a programme.

What does uncaught SyntaxError invalid or unexpected token mean?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.


2 Answers

Try specifying the UTF-8 charset on the HTML host document :

<meta http-equiv="content-type" content="text/html; charset=UTF8">

D3 contains UTF-8 symbols (like π) invalids in non-UTF8 documents.

like image 81
Laurent Jégou Avatar answered Sep 19 '22 10:09

Laurent Jégou


That sounds like a problem with encoding. I recommend The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Despite the somewhat condescending title, it contains some very useful information. Specifically, it sounds like your server is serving the d3.v3.js file with the wrong encoding.

like image 21
T.J. Crowder Avatar answered Sep 18 '22 10:09

T.J. Crowder