30

I am using Jquery for getting a json object from a solr server. When I run my html file with Tomcat it is runns fine but when I embed it with my project which is running on weblogic it gets this error: (debugging done through firebug)

$ is not defined
$(document).ready(function(){  

Why do I get this error when I embed it in my project?

This is the contents of my <head> tag, It is how I include jquery.js:

<head>
  <title>Search Result  </title>
  <style>
    img{ height: 150px; float: left; border: 3;}
    div{font-size:10pt; margin-right:150px;
    margin-left:150px; }
  </style>

  <script type="text/javascript" src="jquery-1.6.1.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){       //Error happens here, $ is not defined.

    });
  </script>
</head>
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Romi
  • 4,833
  • 28
  • 81
  • 113
  • 7
    Did you load jQuery? Did you load it before your custom javascript? – igorw Jun 14 '11 at 08:54
  • 3
    Use jQuery(document).ready(function() instead of $(document).ready(function() . i think it works.. – Ravichandran Jothi Jun 14 '11 at 08:56
  • How are you including jQuery library? Check your path is correct. If you are using a CDN ensure it can be "seen" from your network. – Dan Diplo Jun 14 '11 at 08:56
  • yes i have included jquery.js and before custom java script. – Romi Jun 14 '11 at 08:56
  • first look for jquery file if that is loded then your code must be written after jquery load line – Vivek Jun 14 '11 at 08:57
  • i am getting this error too: NetworkError: 404 Not Found - http://localhost:7001/webProject/jquery-1.6.1.js" , But i have included jquery.js in my html file as well as it is available with in the same directory as my html file – Romi Jun 14 '11 at 09:00
  • yeah!!! that's the problem it means that your js file has not included..see for your js path. – Vivek Jun 14 '11 at 09:02
  • @Romi: post your `head` code, are you 100% sure you included correctly? – Dan Abramov Jun 14 '11 at 09:07
  • Download Firebug (a firefox addon), you can see in the console what absolute url is used to request the javascript file. Then you'll be able to see whether it's correct or not. – Kokos Jun 14 '11 at 09:08
  • jquery.js is with in the same directory as my .html. and i included it as – Romi Jun 14 '11 at 09:09
  • Look at my edit, and i am using firebug, after using it i could say taht i got error NetworkError: 404 Not Found - localhost:7001/webProject/jquery-1.6.1.js"; – Romi Jun 14 '11 at 09:13

11 Answers11

32

Did you load jQuery in head section? Did you load it correctly?

<head>
   <script src="scripts/jquery.js"></script>
   ...
</head>

This code assumes jquery.js is in scripts directory. (You can change file name if you like)

You can also use jQuery as hosted by Google:

<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
   ...
</head>

As per your comment:

Apparently, your web server is not configured to return jQuery-1.6.1.js on requesting /webProject/jquery-1.6.1.js. There may be numerous reasons for this, such as wrong file name, folder name, routing settings, etc. You need to create another question and describe your 404 in greater details (such as local file name, operation system, webserver name and settings).

Again, you can use jQuery as provided by Google (see above), however you still might want to find out why some local files don't get served on request.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • when i used this my code is running fine it means there is a problem with the path but i am not getting why is so :O – Romi Jun 14 '11 at 09:28
  • 1
    @Romi, you're missing `http://` or `https://` from the beginning of the URL. Without it, the browser looks for the asset at the CURRENT folder. – Nathan J.B. Apr 22 '13 at 20:06
  • Or [`//ajax.googleapis.com` for that purpose](http://stackoverflow.com/q/550038/458193). – Dan Abramov Apr 23 '13 at 18:06
  • Instead of loading jQuery in head, you only need to load jQuery before it. – Pbinder Nov 06 '16 at 15:40
15

I found we need to use noConflict sometimes:

jQuery.noConflict()(function ($) { // this was missing for me
    $(document).ready(function() { 

       other code here....

    });
});

From the docs:

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them.

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
abilash kumar
  • 179
  • 1
  • 9
6

You only get this error if jQuery is not correctly loaded, you can check with firebug what url its trying to load so you can see if your relative path is wrong.

Also, on a separate note, you can use $(function(){ as a shorthand to $(document).ready(function(){

Kokos
  • 9,051
  • 5
  • 27
  • 44
3

If you have a js file that references the jquery, it could be because the js file is in the body and not in the head section (that was my problem). You should move your js file to the head section AFTER the jquery.js reference.

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
    <script src="myfile.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
live-love
  • 48,840
  • 22
  • 240
  • 204
0

I have same issue ... at http://www.xaluan.com .. but after log. I find out that after jQuery run I make a function using one element id which not exists ..

Eg:

$('#aaa').remove()  <span class="aaa">sss</spam> 

so the id="aaa" did not exist .. then jQuery stops running.. because errors like that

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Binh Nguyen
  • 1,313
  • 3
  • 17
  • 27
0

Use:

jQuery(document).ready(function($){
    // code where you can use $ thanks to the parameter
});

Or its shorter version:

jQuery(function($){
    // code where you can use $ thanks to the parameter
});
ericek111
  • 575
  • 7
  • 15
Reign.85
  • 2,420
  • 1
  • 28
  • 28
  • What is the difference between your code ans code in question? –  Nov 28 '16 at 12:57
  • The diff is I define $ as parameter. Its an old answer but I remember to got an issue with Jquery, $ was not defined. Actually now I guess using var $ = jQuery should works too... – Reign.85 Nov 28 '16 at 16:01
0

I just had this issue and it was because of me trying to included jQuery via http while my page was loaded as https.

Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39
0

I had this same problem in Chrome where my scripts were like:

<script src="./scripts/jquery-1.12.3.min.js"></script>
<script src="./scripts/ol-3.15.1/ol.js" />
<script>
...
$(document).ready(function() {
    ...
});
...
</script>

When I changed to:

<script src="./scripts/jquery-1.12.3.min.js"></script>
<script src="./scripts/ol-3.15.1/ol.js"></script>

$(document).ready was called.

0

see for your js path that may be the causing issue...because You only get this error if jQuery is not correctly loaded.

Vivek
  • 10,978
  • 14
  • 48
  • 66
0

You can also write your jquery code in this function. document.addEventListener("DOMContentLoaded",function(),{ },false);

-1

Set events after loading DOM Elements.

$(function () {
        $(document).on("click","selector",function (e) {
          alert("hi");

        });

    });