1
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>

above written is the code i am using for testing jquery..

when i run the html file its not working in my browser.

this code runs well in my browser when i run it from w3schools.com.

please let me know what could be the reason for not working of this code. the java script is enable in my browser.

8
  • 1
    Did you try to use a debugger to see what fails? Feb 27, 2013 at 7:37
  • 3
    "...jquery code not working..." "...the code is perfect..." Those two statements don't really go together. Feb 27, 2013 at 7:40
  • 2
    Is your URL address strating with "file:///..." ? Feb 27, 2013 at 7:42
  • 2
    EVERYONE: The script path is fine (provided the HTML page isn't being loaded locally, e.g. from a file:// URL). By leaving off the protocol, the OP is making it work on both an http and https page. Yes, really. Feb 27, 2013 at 7:42
  • 1
    Yes, Really. Thanks Crowder. Lay down the law :)
    – phareim
    Feb 27, 2013 at 7:51

3 Answers 3

3

Your code works as expected.

But. How are you opening the file? Through the file-explorer, or are you hosting the file on a local web-server?

The first won't work, the latter will, because of the way you're doing the jQuery script file path. By leaving off the protocol, you make it inherit the protocol of the page. If that's http or https, that's fine. But if it's file, well, you probably don't have a file at that location. So if you're loading it locally (e.g., you see file:// at the beginning of the URL), you need to put a http: in front of your script path.

Alternately, if you are looking for an easy to set up webserver on Windows (so you can open the file with a http URL and your script path works), I'm fairly happy with QuickPHP. Linux/Windows/Mac all have lots of options. Just ask Google.

Best of luck!

3
  • Wow. Thanks for the edit, Crowder. I can see where the 165k comes from.
    – phareim
    Feb 27, 2013 at 7:53
  • Thank you phareim.. http:// was missing in the script. it worked locally when i wrote http:// in script path. Thanks to u too crowder. Feb 27, 2013 at 8:36
  • @user2034961 no problem. For further development I encourage you to set up some form of web-server locally. - Happy hacking!
    – phareim
    Feb 27, 2013 at 8:50
1

Your jQuery path can only work if your file is on the server. If not then use this path:

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

0
-2

The <script> source is wrong

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
2
  • yeah the source was wrong i corrected it now..thanks for replying. Feb 27, 2013 at 8:40
  • please mark it as answer if you got the right suggestion from the post. Thanks
    – K D
    Feb 27, 2013 at 8:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.