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





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.