Handling errors in jQuery.getScript

前端 未结 6 822
陌清茗
陌清茗 2021-01-02 14:00

jQuery\'s getScript function doesn\'t seem to support an error callback function. I can\'t use the global ajax error handling code here, a local error function would be idea

6条回答
  •  攒了一身酷
    2021-01-02 14:16

    As of jQuery 1.5 you can append a .fail to your call to getScript.

    $.getScript('foo.js', function(){
        //script loaded and parsed
    }).fail(function(){
        if(arguments[0].readyState==0){
            //script failed to load
        }else{
            //script loaded but failed to parse
            alert(arguments[2].toString());
        }
    })
    

    http://api.jquery.com/jQuery.getScript/#handling-errors

提交回复
热议问题