In this post, you will know the reasons behind having the ReferenceError in your console log. Also we will discuss some common case with examples.
There is a common javascript error like this line: you are trying to access a variable or call a function that has not been defined yet.
Reproducing the error:
// Variables
name; // ReferenceError: name is not defined
var name;
name; // No more errors
// Functions
getData(); // ReferenceError: getData is not defined
getData = function(){};
getData() // No errors
The reason behind "ReferenceError: $ is not defined" error is same as the error above except the name of the function is $ instead of getData.
That means if you try to call or access the method that does not exist or we can say that is not defined yet, the reference error comes. So, the $ is not defined error comes only when jquery file has not been loaded before accessing $ function.
Here are some common cases when error may occur:
1. When there is no correct path of your jQuery file.
<script src="/wrong/path-to/jquery.min.js"></script>
Solution to this problem is including correct path.
<script src="/correct/path-to/jquery.min.js"></script>
2. When you include jQuery file without protocol in the URL and try to access the web page from your local system.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
Solution to this problem is add HTTP protocol (http://)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
0 Comment(s)