Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run document.ready only if jquery is loaded

Tags:

jquery

I have this code in a global JS file.

$(document).ready(function() {
       DoStuff();
 });

Unfortunately there are pages that include this file but doesn't include the jquery file. So I get errors:

Message: The value of the property '$' is null or undefined, 
not a Function object

How do I run document.ready() only if JQuery file is loaded.

like image 455
DotnetDude Avatar asked Jun 04 '26 01:06

DotnetDude


2 Answers

if(window.jQuery) {
    $(document).ready(function() {
        DoStuff();
    });
}

UPDATE

Check for window.jQuery instead of just jQuery. This should circumvent the "not defined" error.

like image 69
bfavaretto Avatar answered Jun 05 '26 16:06

bfavaretto


You would test for the existence of jQuery first:

 if(jQuery)
 {
     $(document).ready(function() { ... });
 }
like image 31
Tejs Avatar answered Jun 05 '26 16:06

Tejs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!