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
3 Answers
Reset to default 91) 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.