Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

file

local

Let's say this my page for example ..

<!doctype html>
<html>
  <head>
  </head>
  <body>
    <script src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
      });
    </script>
    <div id="div1"></div>
  </body>
</html>

As you see I'm using a local jQuery file (jquery.js) so every time I write jQuery code it doesn't work knowing that both of my page and jquery.js in same level ..

But when I'm using a jQuery file online, like this for example

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

everything works fine..

Also I tried to put <script></script> in the <head> but nothing works

P.s. I downloaded the same file I'm using online and using it locally.

like image 985
Sam Avatar asked May 08 '11 09:05

Sam


2 Answers

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.

like image 195
T.J. Crowder Avatar answered Nov 14 '22 23:11

T.J. Crowder


The browser should not care whether the file is local [local to the webserver, presumably...?].

Something is wrong with the copy of the file that you have locally. Ensure that the path is correct and use Firebug to see what happened to its loading.


Edit For your demo_local, my Firebug installation complains of foreign characters in the jquery.js file. When I look on the Script tab, it appears to be in Unicode but is served as ASCII.

like image 43
Lightness Races in Orbit Avatar answered Nov 14 '22 22:11

Lightness Races in Orbit