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

后端 未结 11 1262
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条回答
  •  独厮守ぢ
    2020-12-03 03:39

    Seems like jQuery handles cross-domain and same domain requests just fine now. For cross domain requests, it will add a script tag and subscribe to error and load events and appropriately call your done and error callbacks, it's an error only if a request fails, errors evaluating the script will still be considered a success as far as the request is concerned. For same-origin, it will do an XHR request and then do a globalEval and once it's done, call your done callback, if anything fails in the process, it will call your error callback.

    Using XHR has a few issues, it breaks the debugger, sourcemaps and it relies on a globalEval, which has issues with ContentSecurityPolicy. For that an consistency, we simply add a script tag when we load scripts.

    var scriptElement = $("

提交回复
热议问题