Test if jquery is loaded not using the document ready event

◇◆丶佛笑我妖孽 提交于 2019-11-29 10:22:16

I think you could just use

if (jQuery) {
   ...
}

to see if the jQuery object exists.

Ok, better would be:

if (typeof jQuery !== 'undefined') {
   ...
}

or

if (typeof jQuery === 'function') {
   ...
}

EDIT:

Don't worry about the overhead or whether the jQuery object is loaded. If you just include the jQuery library using a regular <script src="..."> tag and then execute your code without the $(document).ready, like this:

<script type="text/javascript">
   $.getJSON(_secureHost + '/base/members/current.aspx?callback=?', function (data) {
        initPage(data);
    });
</script>

It will work. The $(document).ready part is only to ensure the DOM has fully loaded before you go around trying to change DOM elements that aren't loaded yet. The jQuery library itself, including the Ajax functionality, will be there right away.

Now, if your initPage(data) call uses the DOM, which I assume it does, you could put a check in that, like this:

<script type="text/javascript">
    function initPage(data) {
        var $domObject = $('#your-dom-object');
        if ($domObject.length === 0) { // Dom object not loaded yet.
            window.setTimeout(function() {
                initPage(data);
            }, 0); // Try again after other stuff has finished.
            return;
        }
        // Do your regular stuff here.
    }
</script>

However, I don't think this would be necessary in most situations.

As long as you place the script tag that includes jQuery above your script tag that runs your function then it should work fine.

Wrapping your script in this instead of the ready function may help:

(function() {
    // Your code here
})();

You can add either load complete function or grid complete function like shown below which will help you to do some operation on jqgrid

 height : '550',
    width : '787',
    loadComplete : function() {}
    gridComplete:function(){}

hi I think You should first check that is jquery and the method you are going to use is defined or not

if(typeof(jQuery)!='undefined' && typeof($.ajax)!='undefined' ){
// your code will come here
}

ifaour

What I don't like about the above call, is that the jsonp can actually be exectued before the document ready event

are you sure about that?! please note you may still have images loading..etc
I guess you need to use:

$(window).load(function() {
    $.getJSON(_secureHost + '/base/members/current.aspx?callback=?', function (data) {
        initPage(data);
    });
});  

Link

I was using footable jquery plugin. This worked for me :

if ( $.fn.footable) 
{...}

jquery version - 1.9.1 footable version - 0.5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!