jQuery code doesn’t work if I’m using a local jquery.js file, why?

It’s an encoding problem. Your page.html file is in UTF-16 (although it contains an erroneous meta tag saying that it’s using UTF-8), but the jquery.js file isn’t (it’s either in ASCII, Windows-1252/ISO-8859-1, or UTF-8 — doesn’t really matter which, it sticks to the characters all of them have in common).

I suggest correcting the encoding of page.html (making it actually UTF-8). That will probably clear it up. But if not, or if you really want UTF-16 (e.g., you’re doing a page mostly in non-Western script) you can use the charset attribute on the script element to tell the browser what to expect when fetching the script.

Here’s how I got there: When I visit the link you posted, I get an “illegal token” error in jquery.js in the console (on Chrome and in Firebug) and the jquery.js file content shown is garbled, showing mostly in an east asian character set. If I request the resource directly, I don’t have that problem. That immediately made me think “encoding problem” and go look at page.html. A quick glance revealed it to be in UTF-16. Double-checked that the jquery.js file wasn’t also in UTF-16 (it could be, though I would never encode JavaScript that way, it would be very wasteful) and found it to be in an ASCII-compatible encoding (e.g., not UTF-16).

If you’re not 100% certain you understand what I’m saying about page.html being in UTF-16 but jquery.js being in UTF-8 or similar, I’d recommend reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky and also the various FAQs on unicode.org, in particular the main one and the one discussing the various UTFs and the BOM.

Leave a Comment