6

My question isn't so much about why a specific bit of jQuery I've written isn't working as it is about no jQuery at all is working; not even working examples I've copied directly from places like W3 Schools.

I use jQuery from time to time in my software development job and while I am by no means an expert, I am pretty familiar with it. For the first time I am trying to use jQuery in a home project and no matter what I do, none of it will work. The example I've included below is about as simple as I can think of, and even it will not work.

<!DOCTYPE HTML>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<div>
    <p>Old Stuff</p>
</div>
<script type="text/javascript">
   $('p').text('New Stuff');
</script>
</body>
</html>

What could possibly be wrong with this?

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
jtrohde
  • 412
  • 2
  • 4
  • 16

6 Answers6

8

The code is ok.

The script is not downloading because, as you probably are not deploying the code, the browser will default to the file:// protocol.

To solve it, add the http: at the script tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
                                                                           </script>
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • I actually tried it out here. Chrome do not load the script. If `http://` is not specified, it searched for `file://` – acdcjunior May 12 '13 at 16:51
  • @mplungjan if `http:` is not there won't browsers look for file relative to root folder? – Optimus Prime May 12 '13 at 16:52
  • @mplungjan: the protocol *is* required on local machines (for reasons explained by [BoltClock, below](http://stackoverflow.com/questions/16509740/why-does-no-jquery-work-on-my-home-machine/16509764?noredirect=1#comment23701542_16509764)). – David Thomas May 12 '13 at 16:52
  • 1
    @David Thomas: It's because `file://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js` probably won't point to anything on a local machine :) – BoltClock May 12 '13 at 16:54
  • Thanks. I thought it would be something silly like that, just didn't know what exactly. This fixed the issue when opening the file directly with the browser. However, I'm using phpDesigner8 for an IDE, and the jQuery is still not working when I run the file through the IDE. – jtrohde May 12 '13 at 19:25
4

change this

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

to this

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Emre Akay
  • 136
  • 8
1

The way you load jQuery might be a problem. When you start your URL with // it is supposed to work when page is being browsed via HTTP or HTTPS protocols. However, if you will open it as a local file it won't work.

Given that your example works over HTTP I suggest you try to include jQuery as following:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
Sergey Rybalkin
  • 3,004
  • 22
  • 26
0

Use http:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
0

Check if you have the proper permission to the script by invoking the script location in the browser url. So, if you have just put the "http://someloaction/js/jquery.min.js" in the browser url. If you can see "forbidden", permissions are ok.

-2

You forgot to place the

$('p').text('New Stuff');

inside document ready for that you can do like this:

$(function(){
   $('p').text('New Stuff');
});
Milson
  • 1,525
  • 3
  • 15
  • 29
  • Nope. It is after the P so it is ok – mplungjan May 12 '13 at 16:53
  • 1
    But this is not the best Practise to use jQuery so beside the jQuery js file src include in local or CDN we need to do this on document Ready function – Milson May 12 '13 at 16:57
  • We are not discussing best practice. There are times you need to have inline script and some people swear that scripts should be before the /body and others that it must be in the head and so on. The code works, the issue was file:// versus http:// – mplungjan May 12 '13 at 16:58
  • 1
    yup file source is the issue which is obviously can be find by using FireBug addon in firefox and in Console Running $ it will generate undefined that the way to test the jQuery is initialized or NOT... – Milson May 12 '13 at 17:01
  • Thanks. I realize this is not best practice. I just coded things this way so things would be as simple as possible. – jtrohde May 12 '13 at 19:16
  • gud job @jtrohde all the best for becoming a jQuery Master :) – Milson May 13 '13 at 01:53