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

后端 未结 11 1297
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:51

    Yeah, I've discovered too that getScript is unreliable in FireFox, firing the callback before the script has been downloaded and/or executed. (Using JQuery 1.41 & FireFox 3.6. Issue doesn't seem to afflict IE, Chrome or Opera.)

    I haven't done extensive testing, but it seems to only happen with some specific scripts...not sure why.

    RaYell's suggestion doesn't work, as getScript will report success even though the script has not yet been eval'ed. Pieter's suggestion in most cases causes the code to be eval'ed twice, which is inefficient and can cause errors.

    This alternative seems to work where getScript does not. Note that it seems to be adding a SCRIPT DOM element, whereas getScript does XHR.

    http://www.diveintojavascript.com/projects/sidjs-load-javascript-and-stylesheets-on-demand

提交回复
热议问题