In this post, we will see how to resolve uncaught referenceerror: $ is not defined
jquery error.
Table of Contents
$
represents jQuery function
. You will get this error when you are trying to access anything before loading jQuery
.
request //uncaught referenceerror: $ request is not defined
var request
request // works fine
There can be multiple reasons for this issue.Let’s understand them one by one.
1. You did not add jQuery script
You did not add jQuery script to your PHP/JSP/ASP file correctly. You can use directly link it to jQuery or GoogleCDN or MicrosoftCDN to use jQuery script.
1 2 3 |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> |
You can also download jQuery script and reference it locally for faster performance.
2. You are using jQuery tag before jQuery is loaded.
It could be that you have your script tag called before the jquery script is called.
For example:
1 2 3 4 |
<script type="text/javascript" src="js/script.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> |
It will result in $ is not defined
.
Change the sequence and it should work fine.
1 2 3 4 |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="js/script.js"></script> |
So dependent script should be included after jQuery.
Let’s see another example
Let’s say jquery.plugin.js
is declared before jquery.min.js
, so jquery.plugin uses $
, it will complain that $
is not defined which is logical as jQuery
was not loaded at correct place.
1 2 3 4 |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.plugin.js" type="text/javascript"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> |
Change the sequence and it should work fine.
1 2 3 4 |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.plugin.js" type="text/javascript"></script> |
3. Incorrect path/Typo/old jquery location
You might have put an incorrect path, or there may be typo in your file.
You have misspelt in script as
type="text/javacsript"
. If you notice that here, there is a typo
in javascript spelling, and it can cause this issue.Another reason may be that you are referring to an old jQuery hosted location, which is no longer maintained or moved to a different location.
4. You are offline
This seems bizarre but this may be also the reason for this error.You are working offline but trying to load jQuery from internet.
You can simply download the jQuery.js
and use it locally rather then downloading from internet.
1 2 3 |
<script src="/js/jquery.min.js"></script> |
That’s all about how to fix uncaught referenceerror: $ is not defined
jquery/javascript error.