最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Anonymous Function Error: $ is not defined - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Feb 1, 2015 at 17:06 ArchitectArchitect 1031 gold badge2 silver badges7 bronze badges 4
  • 3 It probably means that the code runs before jQuery is imported. – Pointy Commented Feb 1, 2015 at 17:09
  • 2 "What causes the error?" Your access to the undeclared variable $. "Can it or should it be ignored?" Errors should never be ignored. "The page and the script work as required. " Then you don't even needs this script, since it doesn't run either way. – Felix Kling Commented Feb 1, 2015 at 17:09
  • The page and the script work as required. Are you sure? How do you debug it? I meant if $ is undefined, the click handler cannot be fired – A. Wolff Commented Feb 1, 2015 at 17:10
  • Please run a proper browser/debugger (like Chrome) that will report the exact line of your source where the error occurs. Then, and only then, can you properly troubleshoot what is causing the error. – jfriend00 Commented Feb 1, 2015 at 17:25
Add a ment  | 

3 Answers 3

Reset to default 9

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./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./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.

发布评论

评论列表(0)

  1. 暂无评论