I am writing a javascript file for an ASP.NET web app used by many people. This file will use jQuery, so to ensure I can safely use the $ alias, the file is structured like this:
(function ($) {
// My javascript here can safely use the $ alias
})(jquery);
However, my Firebug console is showing the error “jQuery is not defined”. I made sure the script tag for this file comes after the script tag for jQuery, but clearly this script is running before jQuery is ready.
How can I ensure the script runs after jQuery is ready?
Identifiers in Javascript are case-sensitive.
The variable
jquerydoesn’t exist, butjQuerydoes.To address your doubts about when scripts are executed:
The jQuery script will always be loaded and executed before the browser continues with script later in the page. (Unless of course the jQuery script fails to load altogether.)