Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...