Click here to Skip to main content
11,634,622 members (67,615 online)
Click here to Skip to main content

Tagged as

Checking if jQuery is loaded vs. ready

, 3 Oct 2011 CPOL 23.1K 7
Rate this:
Please Sign up or sign in to vote.
This tip helps you to check if jQuery will be available to your script.

I am currently developing an ASP.NET User Control. My control has dependency on jQuery, so in order for my control to operate correctly and be robust, I needed to check that jQuery has/will be loaded.

First let's define the two states of jQuery:

  • loaded: jQuery is referenced in the current page by way of <script></script> tags.
  • ready: jQuery is loaded and the DOM is ready for manipulation.

A non-denominational web search led me to several articles that suggested one of the following constructs:

if(jQuery) alert('jQuery is loaded.');
if(typeof(jQuery) != 'undefined') alert('jQuery is loaded.');

That makes sense; however, these simply don't work — at least not in all browsers. In actual practice, these tell us if jQuery is ready, not loaded.

Instead, I determined that the most reliable means of checking if jQuery is loaded is to check if the $ function is defined:

if(typeof($) == 'function') alert('jQuery is loaded.');

Note: this will yield a false positive in the unlikely event that the $ token has been defined by something other than jQuery as a token.

I like to use this anywhere that the standard jQuery $(document).ready() construct is used:

if(typeof($) != 'function') alert('This component requires jQuery.'); 
else $(function() { alert('The DOM is ready for jQuery manipulation.'); });


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yvan Rodrigues
President Red Cell Innovation Inc.
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the manufacturing sector. He runs Red Cell Innovation Inc./L'innovation de Globules Rouges, a consulting company focusing on efficiency and automation of manufacturing and business processes for small businesses, healthcare, and the public sector. He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience at Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.

Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.

You may also be interested in...

Comments and Discussions

QuestionThank you! Pin
Member 96030251-Apr-14 5:39
memberMember 96030251-Apr-14 5:39 
AnswerRe: Thank you! Pin
Yvan Rodrigues1-Apr-14 5:40
professionalYvan Rodrigues1-Apr-14 5:40 
GeneralGood job..thanks Pin
kiran dangar19-Oct-11 23:10
memberkiran dangar19-Oct-11 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid