Test if jquery is loaded not using the document ready event

后端 未结 6 1111
北海茫月
北海茫月 2020-12-19 05:28

On my site I have a jquery function which retrieves data from another (secured) server as soon as the page is loaded. Using a jsonp call I currently load this data after doc

6条回答
  •  抹茶落季
    2020-12-19 05:48

    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

    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:

    
    

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

提交回复
热议问题