-1

My code is like this. But it is not working on any browser. this code has been copy-pasted from w3scools. Same is the condition with my other codes.

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
     $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>

tckmn
  • 57,719
  • 27
  • 114
  • 156
NewCoder
  • 59
  • 1
  • 3

3 Answers3

4

I don't know if this will work, but try adding http: to the beginning of your link reference. Additionally, as others have pointed out, close your <html> tag.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
     $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

Another thing I would suggest, is using jQuery's CDN for jQuery hosting. The URL's a lot shorter, and it gets you the latest version without having to define the version:

<script src="http://code.jquery.com/jquery.min.js"></script>

However, I see you're using version 1.8.3, which could be for a specific reason, so just add the version you want after jquery in the url. jquery-1.8.3.min.js.

ModernDesigner
  • 7,539
  • 10
  • 34
  • 41
1

The link to your jQuery script is not working. Replace it with:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

Instead.

It wasn't working for me with the old link, but after replacing it with the other link it worked just fine.

(I'm assuming you noticed the missing </html>.)

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 2
    `w3fools.com` - The outdated website that tells w3schools to wikify their content, while not wikifying their own website. Oh the irony. – Derek 朕會功夫 Feb 09 '13 at 21:46
  • http://stackoverflow.com/questions/14792954/jquery-load-not-working-on-phonegap @DOORKNOB can you plz help me wid dis? – NewCoder Feb 09 '13 at 23:58
0

Your code actually does work (even without the missing html closing tag :-P) but only if it is run from a server. It is all to do with the URI for the jQuery file.

If you run the file locally, the browser guesses that it should download the file using the file protocol, so:

file://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js

If you open up your browser's developer tools (by pressing the F12 key usually), it should show you an error saying the browser couldn't load the jQuery file from a file:// URL. If you put http (or https) as the protocol, it knows where to get jQuery from and everything works.

Interestingly if you run the file on a server (which I guess is what w3schools intended) then the browser guesses it should be the http or https protocols and it finds the jQuery file.

If you want to experiment running a webserver without putting in much effort, Python (because it is awesome) lets you run a web server with just one command:

python -m SimpleHTTPServer

It runs the server on:

http://0.0.0.0:8000 

and serves the files from the directory where you run it. This url has a little more detail about it: http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python

I wasn't aware of the server requirement (thanks for the question!) and I found this in my travels which I thought was interesting. It talks about how the missing protocol is valid html StackOverflow: Is it valid to replace http:// with // in a script tag?

Community
  • 1
  • 1
Ben
  • 2,143
  • 2
  • 19
  • 27