1
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$('div'); // <--- THIS DOESN'T WORK
</script>

I'm getting an error in the above code. And when checking in the .js code I can't find a function named $, but according to the documentation there's supposed to be one.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Dude Dawg
  • 1,465
  • 2
  • 15
  • 26
  • 1
    *'I had to remove the lt and mt symbols around "script"'*. Instead just learn to format code on StackOverflow. There's a `?` button above the edit area if you still don't know how. It's really easy. –  Apr 14 '12 at 19:11
  • So, what's the error you are getting? – Evan Mulawski Apr 14 '12 at 19:11
  • Both, when my code wasn't working I reduced it into the smallest non working example that still had a problem. – Dude Dawg Apr 14 '12 at 19:23
  • The question I answered just disappeared. – Dude Dawg Apr 14 '12 at 19:24
  • Lol, Sierra: World War 2: Prisoners of War: "I must be seeing things", lol. For some reason script tags disappear and questions and replies disappear into thin air too. I like stackoverflow, but it's a little rough around the edges. – Dude Dawg Apr 14 '12 at 19:26

3 Answers3

5

Try http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js. Notice the http: in the beginning.

Even though it should work without that as well. I guess you were trying to open your HTML locally (not on a server through HTTP) through file://. In that case, of course a schemeless URL won't work.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
  • Thanks, that works. I wonder why '//' alone wont work, according to some article I just read it's a trick that helps you if you don't know if the site is http or https. – Dude Dawg Apr 14 '12 at 19:15
  • It isn't required? Maybe my web browser is bad or something, because when I added it works. Firefox 11.0 or 1.0, the "about" has it both ways. – Dude Dawg Apr 14 '12 at 19:17
  • @DudeDawg Well, it SHOULD work. Please see the link I added to my question. – kapa Apr 14 '12 at 19:19
  • 3
    `src="//...` (protocol-neutral) will work just fine if the file is actually being served by a webserver, but not if you're using the file protocol to view the file locally. – steveax Apr 14 '12 at 19:21
  • @steveax Thanks. My quick research found the same possible caveat. Added it to my answer. – kapa Apr 14 '12 at 19:24
  • @am not i am, That's a usefull site. – Dude Dawg Apr 14 '12 at 19:28
  • @bažmegakapa OK so it should work? I blame firefox for my troubles then. – Dude Dawg Apr 14 '12 at 19:29
  • @DudeDawg Please read my updates. It won't work in any browser (not just Firefox) if you open your HTML locally (`file://`). – kapa Apr 14 '12 at 19:30
-1
<script type="text/javascript">
$(function(){ // this is equivalent to $(document).ready(function{
    $('div'); 
});
</script>

You need to make sure your code runs after jQuery and the page have finished loading.

Emyr
  • 2,351
  • 18
  • 38
  • 1
    This is automatic. The `$(function() {})` isn't needed to ensure jQuery is loaded, just the DOM. If it was, then the first `$` still wouldn't work. –  Apr 14 '12 at 19:14
  • No, this is not true. You do this to be sure the DOM is ready when you run your code. ` – kapa Apr 14 '12 at 19:14
-1

I think it depends on where you are including these script tags. If they are in your head before your opening body tag, then you would need to wrap the second statement in the document ready function to ensure the DOM is loaded before jQuery starts looking for 'div'.

<script type="text/javascript">
$(function(){
    $('div'); 
});
</script>

Otherwise, if your script tags are just before your closing body tag, this should work.

knaffles
  • 23
  • 3
  • No, this is not true. You do this to be sure the DOM is ready when you run your code. – kapa Apr 14 '12 at 19:26
  • Right -- so regardless the first script which calls the jQuery library always needs to precede the second. But the document ready function in the second is only required if both scripts are in the head as opposed to the end of the body. Does that sound right? – knaffles Apr 14 '12 at 20:00
  • Wherever they are, the question has the scripts in the right order, so the example should work (at least return an empty jQuery collection, but never throw an error). – kapa Apr 14 '12 at 20:04