loading javascript file after jquery.ready

后端 未结 4 1422
轮回少年
轮回少年 2020-12-28 22:27

I want to load a javascript file at the end of jquery.ready so that the code in my ready handler doesn\'t have to wait to execute until this large javascript file is loaded.

4条回答
  •  粉色の甜心
    2020-12-28 23:00

    solution will check jquery already loaded, if not it will check after some time here 500ms and loop until it found jquery

    function loadScriptAfterJQueryReady(jsFile) {
        setTimeout(function () {
            var loadScript=true;
            if (typeof jQuery == 'undefined') {
                if (typeof window.jQuery == 'undefined') {
                    /* jQuery is not loaded */
                    loadScript=false;
                    loadScriptAfterJQueryReady(jsFile);
                }else{
                    /* jQuery is loaded */
                }
            } else {
                /* jQuery is loaded */
            }
            if(true==loadScript) jQuery.getScript(jsFile);
        }, 500);
    }
    loadScriptAfterJQueryReady("my.js");
    

提交回复
热议问题