Uncaught SyntaxError: Unexpected token ILLEGAL - when splitting up a file - javascript

I have one big file that consists of HMTL, Javascript and CSS and I wish to split it up to make it more managable. But when I split the HTML file up and save it as seperate HTML, Javascript and CSS files I get the error :
Uncaught SyntaxError: Unexpected token ILLEGAL
When I click on the error it takes me to the file in the browser and my file consists of only Chinese symbols.
I have tried finding the answer and apparently I have hidden characters in my code but how am I meant to find these ?
What can I do to fix this issue ?

Mojibake like this can occur when the program reading in a file is unable to figure out what kind of encoding a file is using and guesses the encoding incorrectly. It could be that you have a weird character lurking in your file somewhere that is completely throwing off the encoding detection.
Some encoding schemes provide a way to mitigate this problem by using a BOM, which is a short marker at the beginning of the file that indicates the type of encoding that the file uses. BOMs have advantages and disadvantages, but one big advantage is that they firmly establish a file's encoding so that the program loading the file doesn't have to guess what the encoding is. It sounds like including the BOM in your file remedied your issue.
For more information on encoding, see:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Related

WordPress loading javascript with strange character set

I'm using WordPress 5.1 with Yoast SEO. Yoast SEO relies on the file components.js which is throwing the following error in the console (I've edited this for brevity - it's a very long string)
Uncaught SyntaxError: Invalid regular expression:
/[A-Za-zªµºÀ-ÖØ-öø-ƺƻƼ-Æ¿Ç€-ǃDŽ-ʓʔʕ-ʯʰ-ʸʻ-ËË-Ë‘Ë -ˤˮͰ-ͳͶ-ͷͺͻ-ͽͿΆΈ-ΊΌΎ-Î
The identical string does not appear in the file, though the file does include the following line when looking with a text editor:
["+"A-Za-zªµºÀ-ÖØ-öø-ƺƻƼ-Æ¿Ç€-ǃDŽ-ʓʔʕ-ʯʰ-ʸʻ-ËË-Ë‘Ë -ˤˮͰ-ͳͶ-ͷͺÍ
The line looks like this when looking through the webhost control panel:
["+"A-Za-zªµºÀ-ÖØ-öø-ƺƻƼ-ƿǀ-ǃDŽ-ʓʔʕ-ʯʰ-ʸʻ-ˁː-ˑˠ-ˤˮͰ-ͳͶ-ͷͺͻ-ͽͿΆΈ-ΊΌΎ-ΡΣ-ϵϷ-ҁ҂Ҋ-ԯԱ-Ֆՙ՚-՟ա-և։ःऄ-हऻ
The only odd thing is that the database had a mix of character sets (latin1, utf8 and utf8mb4) which I have attempted to fix and all tables now use utf8mb4_uncode_ci (this was chosen as it was the most common character set in the db).
There is also a mix of InnoDB and MyISAM table types. The site has a number of  characters around the site which is a common indicator of character set issues as far as I can tell.
So I'm guessing for some reason WordPress is loading the javascript file with the incorrect character set which is creating errors.
Is there a way to fix this? I'm a bit baffled.
Fixed.
This was due to blog_charset being set to UTF-7 in wp_options. Changing this to UTF-8 has solved the problem

js files are loading in unreadable format

I was working on a Web App. I used a "✗" UTF-8 character as a delete button. Eclipse asked me to save file in UTF-8 format i told yes. Everything worked fine. But next day when i ran the app again it is throwing exception "Uncaught SyntaxError: Unexpected token ILLEGAL". When i checked it, all javascript file is loading in unreadable format. See image below.
I tried to replace that character with its UNICODE "✗" and saved all js files in default encoding, but didn't help.
Do anyone know why is this happening?
It sounds like you saved in a different text format than you loaded it and/or loaded it in a different text format that you saved it. Without recommending a tool, I would see if you can use something to change the code-page/text-format and then try stuff.
However, seeing as what you are showing on teh screen is 3rd part stuff, you could just more easily re-download it where you got it the first time, or perhaps unpack it if you saved the installer/zip-file.

Squared Question Mark Sign on CSV file read from JS

I'm reading a CSV file in my JS, but characters with accent (á, ó...) are being replaced with a black square question mark (�).
I always have this sort of problem in PHP, but, i'm using JS and i don't know how to fix that.
The problem is in the UTF8 codification of the file, of the HTML, is there a way to fix this in code?
Thanks
This character is U+FFFD, REPLACEMENT CHARACTER, commonly used to replace invalid data in streams thought to be some Unicode encoding.
For example if you had the text "Résumé" encoded as IS0 8859-1 and wanted to convert it to UTF-16, but told the conversion routine that the text was UTF-8 then the library would probably produce the UTF-16 text "R�sum�" (the other alternative would be to throw an error and not give any results).
Another way these may appear is if a web page declares that it is UTF-8 but it is not actually UTF-8. The browser is likely to do the re-encoding described above and the replacement characters will show up in the rendered web-page, but viewing the source with an editor that ignores or disregards the HTML encoding info will show the characters correctly.
From your comments it looks like your process is something like:
Excel -> export to csv -> process csv in js -> produce html
Windows software typically uses the platform's 'encoding for non-Unicode programs' for encoding eight bit text, not UTF-8. So the CSV file is probably Windows CP1252 (If you're using a version of windows set up for most of the western world), and if your javascript program is reading that data and copying it directly into HTML source that's supposed to be UTF-8, that would cause a problem that fits your description.
What you need to do convert from whatever encoding the CSV is using to UTF-8. Javascript doesn't really have the facilities to do this so your best bet is probably to convert the file after exporting it from Excel but before accessing it in JS.
Other alternatives are to change the encoding the HTML page is using to whatever the csv uses, or to not specify an encoding and leave it up to the browser to guess.

Writing Javascript using UTF-16 character encoding

Here is what I am trying but am not sure how to get this working or if it is even possible -
I have an HTML page MyHTMLPage.htm and I want to src a Javascript from this HTML file. This is pretty straightforward. I plan to include a <script src = "MyJavascript.js"></script> tag in my HTML file and that should take care of it.
However, I want to create my Javascript file using UTF-16 encoding. So, I plan to use the following tag <script charset="UTF-16" src="MyJavascript.js"></script> in my HTML file to take care of that
Now the problem I am really stuck at is how do I create the Javascript using UTF-16 encoding - E.g. let's say my Javascript code is alert(1); I created my Javascript file with the contents as \u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029\u003b but that does not seem to execute as valid Javascript at runtime.
To summarize, here is what I have -
MyHTMLPage.html
...
...
...
<script charset="UTF-16" src="MyJavascript.js"></script>
...
...
...
MyJavascript.js
\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029\u003b
When I open the HTML page in Firefox, I get the error - "Syntax error - Illegal character" right at the beginning of the MyJavascript.js file. I have also tried adding the BOM character "\ufeff" at the beginning of the above Javascript but I still get the same error.
I know I could create my Javascript file as - "alert(1);" and then save it using UTF-16 encoding using the text editor and then the browser runs it fine however is there a way I could use "\u" notation (or an alternate escape character) and still get the Javascript to execute fine?
Thanks,
You are misunderstanding character encoding. Character encoding is a scheme of how characters are represented as bits behind the scenes.
You would not write \u004a in your file to "make it utf-16" as that is literally a sequence of 6 characters:
\, u, 0, 0, 4, a
And if you saved the above as utf-16, it would be represented as the following bits:
005C0075
00300030
00340061
Had you saved it as utf-8 it would be:
5C753030
3461
Which takes 50% of the space and bandwidth. It takes even less to write that character literally ("J"): just a byte
(4A) in utf-8.
The "\u"-notation is a way to reference any BMP character by just using a small set of ascii characters. If you were
working with a text editor with no unicode support, you could write "\u2665", instead of literally writing "♥" and the
browser would show it properly.
If you for some weird reason still want to use utf-16, simply write the code normally, save the file as utf-16 and serve it with the proper charset header.

\u00C2 is not defined error in javascript?

I got this error on following line.
$j(id).dateplustimepicker( "setTime" ,timeVal);
Can you please help me to solve this error?
The error is probably not in this line because no string constants are evaluated there. You wouldn't get this error if, for example, id contained the value.
When you get the error again, open the JavaScript console of your browser and look at the complete stack trace. The innermost frame is where you need to be looking.
[EDIT] Since you found the character in jquery-dateplustimepicker.js, this points to the real cause of the problem.
Every text file on your computer has an encoding. But there is no way to tell which one. The problem you have means: Your text/JS file is in UTF-8 encoding but your web server sends it to the browser with a different encoding. The browser then tries to read it but finds odd characters -> error.
Another reason for the error is that someone edited the file, using the wrong encoding. That can happen on Windows, for example, when you load the file with Cp-125x and saves it as UTF-8.
To check, download the file from the web server and do a binary compare with the original.
I got the answer but i forgot to upload here....Actually i got this problem because, There is  character is present in standard library file of jquery. File name is jquery-dateplustimepicker.js. The character either need encoding otherwise, it creates problem. The character must replace with white space instead of Â.

Categories

Resources