6

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.

8
  • Is the local jQuery file actually getting loaded? What happens if you load it directly in your browser? What does Firebug's "Net" tab have to say about it? Commented May 8, 2011 at 9:46
  • Are you sure the jquery.js filename is correct? Commented May 8, 2011 at 9:48
  • @Pekka this is what firebug says about it cl.ly/6YUl Commented May 8, 2011 at 9:51
  • @Damien yes I'm sure, I just copied the file name and put it my code Commented May 8, 2011 at 9:52
  • Why is it in dropbox? Whats the URL for your page? Commented May 8, 2011 at 9:57

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

okay T.J. charset="UTF-8" solved the problem .. but why could that happened, I always doing that without using the charset
@Sam: The reason is that your page.html file is in UTF-16 (even though it says it's in UTF-8). The browser will assume the same encoding for things that page links to unless it's told otherwise; Dropbox isn't giving back any encoding headers on the request for jquery.js at all. Although adding charset works, I suspect the real problem is that you've inadvertently used UTF-16 on your page.html file. Do you really mean to be using UTF-16?
Now I really understand it .. looks it was a problem with my text editor cl.ly/6ZMr and I change it to cl.ly/6Y5e so it's working now even without charset, thant's T.J. for clearing things up :)
@Sam: Good deal, glad that helped!
2

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.

5 Comments

I'm pretty sure about file name and this what firebug says cl.ly/6YUl and I'm using the same file which I downloaded from the same URL in my post
@Sam: Then the first part of Tomalek's statement is the relevant one: "Something is wrong with the copy of the file that you have locally."
@Tomalak: "T.J." ;-) (Hopefully that's not the second time I've spelled your name wrong, I totally see "Tomalek" even though it clearly has an "a" there.)
@Tomalak That's really weird, I just download the file from here <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> then rename it to jquery.js and put it beside my page without any modifies
Same in Chrome debugger ... I see a lot of strange characters. perhaps adding a Content-Type:application/x-javascript; charset=utf-8 header will work.
0

sam

You have some wierd code in that jquery.

looks like jquery.js locally is wrong

please download it from here

http://code.jquery.com/jquery-1.6.min.js

can you try the following

a standard doc type

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

and change script to

<script type="text/javascript">

</script>

8 Comments

@sam , can you please try the changes i suggested.
@same , might be something wrong with the way you are downloading the jquery file on your machine.
@kobe: Actually, the jquery.js file is just fine. (Grab it directly rather than via page.html to see.) The reason it looks garbled when loaded in page.html is an encoding problem, see my answer for details.
@kobe added dl.dropbox.com/u/5307742/web/demo_1_6_changes/page.html still getting illegal characters
|
-2

I can see you are using dropbox to host your files, which automatically means you cannot make local reference to your files. Try including the full dropbox link and see if it works

7 Comments

Why? If he's requesting page.html from a path on Dropbox (as he appears to be), the path to jquery.js will be on Dropbox as well.
The screenshot posted by the OP indicates a successful download of both resources from Dropbox, I believe..?
I'm working locally but I just uploaded to dropbox to see what's gonna happens and unfortunately the same problem
@T.J. Crowder, you are right. Could this be a browser issue? @Sam what browser have you tested this on?
@boug firefox and safari .. btw just to make things clear I mean by locally just loading the file beside my page.html so it doesn't matter if it's on dropbox or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.