Checking if jQuery is loaded using JavaScript

Let's see how to check whether the jQuery is loaded or not, using JavaScript?
Submitted by Pratishtha Saxena, on June 23, 2022

jQuery is a JavaScript library used to simplify HTML DOM tree traversal and manipulation, and event handling. It is free, open-source software. It is user-friendly and easy to use.

Note: Always remember to apply the CDN link while working with jQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

Sometimes after writing the whole jQuery code, it might not work the way you wanted or might be unsuccessful. It can be possible that the jQuery library is not yet loaded. So, if the jQuery library is not loaded then obviously any commands of jQuery will not return anything.

Then there is a need to check whether the jQuery is loaded or not. For this window.onload can be used as a function, which will initiate as soon as the page loads. A condition can be introduced, i.e., window.jQuery, which checks if the jQuery is loaded on the page or not.

Example 1: Check if jQuery is loaded using JavaScript

<script>
window.onload = function() {
    if (window.jQuery) {
        console.log("Yes, jQuery is loaded.");
    } else {
        console.log("No, jQuery not loaded.");
    }
}
</script>

Output:

Example 1: Check if jQuery is loaded or not

Suppose if the jQuery CDN link is not included in the above example, then the jQuery functions will not work. Hence, the window.onload should return "jQuery not loaded".

Example 2: Check if jQuery is loaded using JavaScript

<script>
window.onload = function() {
    if (window.jQuery) {
        console.log("Yes, jQuery is loaded.");
    } else {
        console.log("No, jQuery not loaded.");
    }
}
</script>

Output:

Example 2: Check if jQuery is loaded or not


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.