Chrome Sources Displaying Javascript in Chinese - javascript

My javascript was working properly and then all of the sudden it wasn't. Chrome give me the Uncaught SyntaxError: Unexpected token ILLEGAL error on line 1 of my JS file called para2.js. I removed script from the file except for the following and I still get the error:
$( window ).ready(function() {
});
My jQuery file is included in the head of my document:
<script src="../shared/jquery.js" type="text/javascript"></script>
<script src="js/para2.js" type="text/javascript"></script>
When I look at my js file in Chrome developer tools on the Sources tab, it displays the script in Chinese:
兪敵祲搨捯浵湥⥴爮慥祤昨湵瑣潩⡮笩⥽
When I view the source of the js file, it looks fine. I have read similar posts that say copy and pasting from JSFiddle can include some hidden characters that may cause it, but I have started from scratch writing this file from a blank text file and copy and pasting nothing. It is strange that it was working and now it is not. Did my server get hacked or something? And help is appreciated.

You should use the charset attribute to indicate which charset you are using.
<script src="../shared/jquery.js" type="text/javascript" charset="UTF-8">

I had the same problem with jquery-1.5.1.min.js. Chrome kept evaluating it as Chinese (scripts worked fine in IE). I finally solved the problem by re-encoding the .js files using "convert to UTF-8" in Notepad++. Note, not UTF-8 without BOM, but UTF-8.

Related

why is jquery not loading

for some reason when i try to load jquery via cdn, it works perfectly
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"
however, when i try to load it using the local files, it does not work
script type="text/javascript" src="javascripts/jquery-1.11.2.min.js"
and displays this error:
Uncaught SyntaxError: Unexpected token < jquery-1.11.2.min.js:1
i must say that my server is running on node.js
it seems you have syntax error with your tag.
try this.
<script type="text/javascript" src="javascripts/jquery-1.11.2.min.js"></script>
It seems as though there is an error occurring inside jquery-1.11.2.min.js. Not in the document which contains the script tag. So, the question is: why does your local copy of jquery-1.11.2.min.js contain a <? The answer is that it most likely doesn't. The request for jquery-1.11.2.min.js is probably resulting in a HTML response, which the browser attempts to load as JavaScript, and fails on the first character. Ensure that javascripts/jquery-1.11.2.min.js is the correct path to your JavaScript file.
It can be shown that this is the case because a SyntaxError is a JavaScript error. MDN says the following (emphasis mine):
A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
So, the error is being thrown by the JavaScript engine, which means that the browser is actually trying to load a script file. As you can see from the error message, the error is thrown at jquery-1.11.2.min.js:1, which is line one of jquery-1.11.2.min.js. You can reproduce the same error message using the following code saved in a file named foo.html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="foo.html"></script>
</head>
<body></body>
</html>
Alternatively, you can view a demonstration of equivalent code here (check your console).

jslint error - #1 Expected '(end)' and instead saw '<!'

I am typing in a v simple example into sublime text. I have the SublimeLinter and JSLint plugins installed, but am getting the following error:
#1 Expected '(end)' and instead saw '<!'.
The code is a simple HTML file, which may be my problem. I don't know.
The code is as follows:
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
If it is true that either linting plugin is causing this error what should I do?
The error is generated by JSLint. By default, it runs on files with extensions .js, .json, .html, .sass, and .less. To prevent the error without disabling or removing JSLint, try removing HTML files from this list.
To do this, open JSLint.sublime-settings. Around line 35 you should see the following property.
,"filename_filter": "(\\.js|\\.json|\\.html|\\.sass|\\.less|\\.html)$"
Erase both |\\.html entries so that you're left with this.
,"filename_filter": "(\\.js|\\.json|\\.sass|\\.less)$"
Save your file. You may need to restart Sublime Text. Also note that you can still run JSLint on an HTML file using the following shortcut.
Control + L

My HTML is not reading the Javascript file in codeacademy editor

I'm learning some jquery using codeacademy and while everything works great within the codeacademy editor, it doesn't work when I upload to a server.
Here is the html in question:
www.arbabmazumdar.com/kudos.html
and here is the js:
www.arbabmazumdar.com/Web_Files/test.js
what am i doing wrong?
1)
<script type="text/javascript" src="../Web_Files/test.js"></script>
You don't actually need the .. part as resources are loaded relatively (i.e. from the same "directory") to the HTML file. Web_Files/test.js would be enough.
2) Your file loads just fine, but you didn't include jQuery, so it fails:
Uncaught ReferenceError: jQuery is not defined test.js:88
Uncaught ReferenceError: $ is not defined kudos.html:19
When something doesn't work it's good to check for errors in the JavaScript console(using Firebug, Chrome Dev tools or IEs Developer Toolbar, etc.)
You have not yet included jQuery !
Include this in <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
in <head></head>

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.
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.
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.
Add 'charset="utf-8"'
<script src="/d3.v3.js" charset="utf-8"></script>
I tried setting the charset in the doc and in the script tag itself, but Chrome doesn't seem to care. Not sure if i'm doing something wrong.
I did find success by running it through uglify with the --ascii-only option first.
UPDATE: Turns out my View->Encoding setting in Chrome was not on Auto-Detect, but some Western encoding. Not sure why, but changing that resolved the problem. Ridiculous that that setting would trump a charset property right on the script tag. Considering that users might be in the same situation and not be able to figure it out, i'll still be using uglify to ensure success.
Check if you have a plus sign between all of your string concatenations, if you do not this error will be generated.

Getting SCRIPT1014: Invalid character in IE from local js file

I'm developing a site which is stored locally, and works great in all browsers I've tested except for any versions of IE.
It gives me "SCRIPT1014: Invalid character" for each js-file which I've included in script-tags like this for instance: <script src="Scripts/jquery-1.9.1.min.js"></script>
This generates the following error:
SCRIPT1014: Invalid character
jquery-1.9.1.min.js, line 1 character 1
If I then click the error to view the file in ie developer tools it looks like this:
?? I?%&/m?{J?J??t??`$ؐ#??????iG#)?*??eVe]f#?흼??{???{???;?N'????\fdl??J?ɞ!????~|?"????
etc
I'd except this to be a common problem (Or don't people ever view locally stored webpages in ie?) but I didn't find much when searching, and what I found didn't help.
How to get around this this?
There is a similar problem here: SCRIPT1014: Invalid character
I would check the browser security settings for local scripts.
You can try adding this to your page and see if it works:
<!-- saved from url=(0016)http://localhost -->
or you can set the 'local intranet' zone to low and disable protected mode if on.
http://msdn.microsoft.com/en-us/library/ms537628(v=vs.85).aspx
Edit: Answer in comments - cleared browser cache.

Categories