简体   繁体   中英

Anonymous Function Error: $ is not defined

Google Developer Tools is displaying the following error when my PHP page uses the content from a javascript file (my_scripts.js):

"Uncaught ReferenceError: $ is not defined scripts.js:1 (anonymous function)"

Content of my_scripts.js

$('a.button').click(function(){ 
    window.location.href = "another_page.php";         
});

The page and the script work as required. Clicking the element links to the requested page, but the error is there.

1) What causes the error? 2) Can it or should it be ignored?

1) It looks like your problem is that jQuery haven't been loaded.

Make sure you load jQuery before scripts.js .

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

2) Errors should never be ignored.

You need to load jquery libary BEFORE! Please download the jquery.min.js at http://www.jquery.com/download/ also, write like this:

$(doucment).ready(function(){

    $('a.button').on('click',function(){ 
        window.location.href = "another_page.php";         
    });

});

If you have already included jQuery on your file, and still its not working you should try,

jQuery(doucment).ready(function(){
   jQuery('a.button').on('click',function(){ 
      window.location.href = "another_page.php";         
   });
});

This is causing as there might be some conflicting js files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM