admin管理员组

文章数量:1022804

I am getting this err

Uncaught ReferenceError: $ is not defined.

I have searched and found that the usual cause of this is not having jQuery declared in the right place but I think I have it right.

Thanks in advance.

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax2.js"></script>

<script type="text/javascript">

var slideimages = new Array() // create new array to preload images
slideimages[0] = new Image() // create new instance of image object
slideimages[0].src = "images/aib.jpg" // set image src property to image path, preloading image in the process
slideimages[1] = new Image()
slideimages[1].src = "images/paddypower.jpg"
slideimages[2] = new Image()
slideimages[2].src = "images/rtegaa.jpg"
slideimages[3] = new Image()
slideimages[3].src = "images/ssgaa.jpg"

$(document).ready(function(){

    function get_weather()
    {
        var p = $("#code").val();

        var u = ($('#u').attr('checked')) ? '&u=c' : '';
        var to_load = 'get_weather.php?p='+ p + u;

        $("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif" align="absmiddle">');

        $("#weather").load(to_load);
    }

    $(window).load(get_weather); // Trigger "get_weather" when the window loads

    // Trigger "get_weather" when the form objects are used
    $("#code").change(get_weather);
    $("#u").click(get_weather); 
});

I am getting this err

Uncaught ReferenceError: $ is not defined.

I have searched and found that the usual cause of this is not having jQuery declared in the right place but I think I have it right.

Thanks in advance.

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax2.js"></script>

<script type="text/javascript">

var slideimages = new Array() // create new array to preload images
slideimages[0] = new Image() // create new instance of image object
slideimages[0].src = "images/aib.jpg" // set image src property to image path, preloading image in the process
slideimages[1] = new Image()
slideimages[1].src = "images/paddypower.jpg"
slideimages[2] = new Image()
slideimages[2].src = "images/rtegaa.jpg"
slideimages[3] = new Image()
slideimages[3].src = "images/ssgaa.jpg"

$(document).ready(function(){

    function get_weather()
    {
        var p = $("#code").val();

        var u = ($('#u').attr('checked')) ? '&u=c' : '';
        var to_load = 'get_weather.php?p='+ p + u;

        $("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif" align="absmiddle">');

        $("#weather").load(to_load);
    }

    $(window).load(get_weather); // Trigger "get_weather" when the window loads

    // Trigger "get_weather" when the form objects are used
    $("#code").change(get_weather);
    $("#u").click(get_weather); 
});

Share Improve this question edited May 7, 2015 at 10:07 kapantzak 11.8k5 gold badges42 silver badges63 bronze badges asked May 7, 2015 at 10:05 nedduffnedduff 971 silver badge10 bronze badges 4
  • where is the jquery file in relation to this html? any 404 errors in your console? – atmd Commented May 7, 2015 at 10:07
  • Do you have jquery file in the root path? – Adil Commented May 7, 2015 at 10:07
  • the file is in the same folder, I was previously getting an error about loading jquery but I fixed that only to get this errot. – nedduff Commented May 7, 2015 at 10:09
  • This error means that jQuery has not been loaded before you're trying to use it. You need to check the path to jquery.js, and also ensure that you're loading that before any other script. – Rory McCrossan Commented May 7, 2015 at 10:12
Add a ment  | 

3 Answers 3

Reset to default 4

The solution:

1) Use Google CDN to load jQuery

<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.5/jquery.min.js"></script>

2) It is patible with jQuery v1.5-v1.7 because most of the methods were deprecated in jQuery 1.8+. This is the reason I've used v1.5 of Google CDN jquery in point 1. Most of the examples on cycle plugin use jquery 1.5.

3) Clear Your browser cache, It is the main culprit most of the times.

4) PLease check the loading of jquery using the code below

if(typeof jQuery!=='undefined'){
    console.log('jQuery Loaded');
}
else{
    console.log('not loaded yet');
}

Probably you called jQuery.noConflict() somewhere.

To solve this, wrap your code like this:

(function($, undefined){

    if( !$ )
    {
        console.log('jQuery failed to load');
        return;
    }

    //jquery code here

})(window.jQuery);

This will prevent the jQuery.noConflict() from affecting your code and you can debug in case jQuery is having some troubles.

Also, watch your 'Network' tab, on the Element Inspector (press F12).

This error will not occur if the file "jquery-1.2.6.min.js" is not present in the location mentioned in your script tag. From your code, the jquery file should be present in the same folder as your startup page.

I'll check the following things when this error occurs

1) Make sure the jquery script file is present in the correct location

2) Name of the jquery file is same as referenced in script tag

3) In browsers console window, there is a debugger or source tab which shows all the script and CSS files used by the page. Check jquery is present under the sources tab of the browser

I am getting this err

Uncaught ReferenceError: $ is not defined.

I have searched and found that the usual cause of this is not having jQuery declared in the right place but I think I have it right.

Thanks in advance.

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax2.js"></script>

<script type="text/javascript">

var slideimages = new Array() // create new array to preload images
slideimages[0] = new Image() // create new instance of image object
slideimages[0].src = "images/aib.jpg" // set image src property to image path, preloading image in the process
slideimages[1] = new Image()
slideimages[1].src = "images/paddypower.jpg"
slideimages[2] = new Image()
slideimages[2].src = "images/rtegaa.jpg"
slideimages[3] = new Image()
slideimages[3].src = "images/ssgaa.jpg"

$(document).ready(function(){

    function get_weather()
    {
        var p = $("#code").val();

        var u = ($('#u').attr('checked')) ? '&u=c' : '';
        var to_load = 'get_weather.php?p='+ p + u;

        $("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif" align="absmiddle">');

        $("#weather").load(to_load);
    }

    $(window).load(get_weather); // Trigger "get_weather" when the window loads

    // Trigger "get_weather" when the form objects are used
    $("#code").change(get_weather);
    $("#u").click(get_weather); 
});

I am getting this err

Uncaught ReferenceError: $ is not defined.

I have searched and found that the usual cause of this is not having jQuery declared in the right place but I think I have it right.

Thanks in advance.

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax2.js"></script>

<script type="text/javascript">

var slideimages = new Array() // create new array to preload images
slideimages[0] = new Image() // create new instance of image object
slideimages[0].src = "images/aib.jpg" // set image src property to image path, preloading image in the process
slideimages[1] = new Image()
slideimages[1].src = "images/paddypower.jpg"
slideimages[2] = new Image()
slideimages[2].src = "images/rtegaa.jpg"
slideimages[3] = new Image()
slideimages[3].src = "images/ssgaa.jpg"

$(document).ready(function(){

    function get_weather()
    {
        var p = $("#code").val();

        var u = ($('#u').attr('checked')) ? '&u=c' : '';
        var to_load = 'get_weather.php?p='+ p + u;

        $("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif" align="absmiddle">');

        $("#weather").load(to_load);
    }

    $(window).load(get_weather); // Trigger "get_weather" when the window loads

    // Trigger "get_weather" when the form objects are used
    $("#code").change(get_weather);
    $("#u").click(get_weather); 
});

Share Improve this question edited May 7, 2015 at 10:07 kapantzak 11.8k5 gold badges42 silver badges63 bronze badges asked May 7, 2015 at 10:05 nedduffnedduff 971 silver badge10 bronze badges 4
  • where is the jquery file in relation to this html? any 404 errors in your console? – atmd Commented May 7, 2015 at 10:07
  • Do you have jquery file in the root path? – Adil Commented May 7, 2015 at 10:07
  • the file is in the same folder, I was previously getting an error about loading jquery but I fixed that only to get this errot. – nedduff Commented May 7, 2015 at 10:09
  • This error means that jQuery has not been loaded before you're trying to use it. You need to check the path to jquery.js, and also ensure that you're loading that before any other script. – Rory McCrossan Commented May 7, 2015 at 10:12
Add a ment  | 

3 Answers 3

Reset to default 4

The solution:

1) Use Google CDN to load jQuery

<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.5/jquery.min.js"></script>

2) It is patible with jQuery v1.5-v1.7 because most of the methods were deprecated in jQuery 1.8+. This is the reason I've used v1.5 of Google CDN jquery in point 1. Most of the examples on cycle plugin use jquery 1.5.

3) Clear Your browser cache, It is the main culprit most of the times.

4) PLease check the loading of jquery using the code below

if(typeof jQuery!=='undefined'){
    console.log('jQuery Loaded');
}
else{
    console.log('not loaded yet');
}

Probably you called jQuery.noConflict() somewhere.

To solve this, wrap your code like this:

(function($, undefined){

    if( !$ )
    {
        console.log('jQuery failed to load');
        return;
    }

    //jquery code here

})(window.jQuery);

This will prevent the jQuery.noConflict() from affecting your code and you can debug in case jQuery is having some troubles.

Also, watch your 'Network' tab, on the Element Inspector (press F12).

This error will not occur if the file "jquery-1.2.6.min.js" is not present in the location mentioned in your script tag. From your code, the jquery file should be present in the same folder as your startup page.

I'll check the following things when this error occurs

1) Make sure the jquery script file is present in the correct location

2) Name of the jquery file is same as referenced in script tag

3) In browsers console window, there is a debugger or source tab which shows all the script and CSS files used by the page. Check jquery is present under the sources tab of the browser

本文标签: javascriptUncaught ReferenceErroris not defined ErrorStack Overflow