13

I thought that I was doing everything right, however I keep getting this error. $(document).ready(); // undefined in the console. I imported my jquery script.

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

        $(document).ready(function(){

            $("div#chat").hide();

        });

        function send_file(){

        }

        function remove_selected(){

        }

        function changeToFile(){

        }

        function chatToProfile(){

        }

        function changeToChat(){

        }
    </script>
Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133

5 Answers5

15

If you're running this file locally (which I suspect you are...), this will attempt to find the referenced file on your local system, which will not be there.

To fix this, instead use this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • what is different here other than the `http:` being added? – bretterer Mar 06 '13 at 04:41
  • @Alexey: His answer was incorrect first. He has since edited it. – Ryan Bigg Mar 06 '13 at 04:42
  • @bretterer: The http means that it will look it up online, rather than on the local file path. Go ahead, try copypasta'ing OP's code to somewhere local and opening it in your browser. You'll see the same error he is seeing. – Ryan Bigg Mar 06 '13 at 04:43
  • it was not incorrect i typed http://// instead of http:// and corrected it. you can check the history. – Nishanth Nair Mar 06 '13 at 04:43
  • @RyanBigg i did copy paste it and tested in local but guess what i did not get any kind of error in firebug...strange..?? – Dipesh Parmar Mar 06 '13 at 04:45
  • @RyanBigg I actually did not know that... I thought with the `//` it would just use the correct protocol not actually look locally – bretterer Mar 06 '13 at 04:45
  • 2
    @bretterer It uses the **current** protocol (`file:///` in the OP's case), not necessarily the **correct** one. – Anirudh Ramanathan Mar 06 '13 at 04:45
3

Need to add http: in your script reference. try this:

<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Nishanth Nair
  • 2,975
  • 2
  • 19
  • 22
  • 5
    That's not an error. It fetches it with the current scheme. If you use `http:` explicitly, you might be introducing a security issue with HTTPS, so in fact, it may be better to *omit* `http:`. – icktoofay Mar 06 '13 at 04:38
  • @Whizkid747 That was quite unfair to you. +1 from me, but `http` is missing sounded like you were saying `//` is invalid. – Anirudh Ramanathan Mar 06 '13 at 04:48
  • 1
    The solution proposed is the same as what Ryan proposed, yes, but Ryan gave an explicit description of one case in which `//` would not work as expected. "`http:` is missing" makes it sound like omitting it is incorrect. In fact, *including* `http:` may be a slight security risk as I pointed out in my first comment, so giving that as a blanket recommendation without knowing the circumstances or giving preconditions, I think, is a bad idea. – icktoofay Mar 06 '13 at 04:50
  • yes, Bad English! :). corrected the sentence. Any time // doesn't work, add the protocol as its pretty much obvious that file is opened locally!. – Nishanth Nair Mar 06 '13 at 05:00
2

i use like this :) and those who says HTTP required. read this article for relative Protocol URL Protocol relative url

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/js/jquery-1.8.3.min.js' type='text/javascript'%3E%3C/script%3E")); //Load the Local file (if google is down for some reason) 
}
</script>
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

Script url that you are using for loading is perfect.
You can check your Url by posting "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" in browser url if you will get the javascript text file then it is working.
I test it and that url is working fine on my side.Error is some where else either there is restriction(security resons) or there is internet connection problem on the system when ever you run that web application on the system.
solution is either use minified copy in you web application and provide the relative path or use

 <script type="text/javascript">
  if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/js/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));
 }
 </script>
Bilal lilla
  • 618
  • 3
  • 8
  • 29
-1

You always can check if jQuery is already loaded in the memory and the library is ready to use:

if (typeof jQuery !== 'undefined') {
  //do whatever here
}
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76