Is the callback on jQuery's getScript() unreliable or am I doing something wrong?

后端 未结 11 1281
Happy的楠姐
Happy的楠姐 2020-12-03 03:17

I\'m using the following bit of script to load another one:

$.getScript(\"CAGScript.js\", function () {
    try {
        CAGinit();
    } catch(err) {
              


        
11条回答
  •  萌比男神i
    2020-12-03 03:48

    I wanted to have something similar to auto_load in PHP implemented in JS, the typical solution is using try-catch on all functions.. when the function is missing we jump to downloading its library.. My solution is taken from Pieter solution as follows:

    function CheckUserPW(username,password){
        try{
           loginCheckUserPW(username,password);
        } catch(err) {
            $.getScript('login.js', function(xhr){ 
                eval(xhr);
                loginCheckUserPW(username,password);
            });
        }
    }
    

提交回复
热议问题