How do I load a variable number of scripts with jQuery.getScript() before running javascript code?

前端 未结 6 553
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 20:10

I need to load a variable number of javascript source files before running javascript code that depends on them. Sometimes 1 script needs to be loaded, other times 2. The ge

6条回答
  •  天命终不由人
    2021-01-12 20:41

    If you are using jquery 1.5 you can use the new deferred syntax.

    $.when(
        $.getScript("1.js"), 
        $.getScript("2.js"), 
        $.getScript("3.js")
    ).then(function(){
        alert("all loaded");
    });
    

    Just pass in the scripts you wish to load.

提交回复
热议问题