how to check the script element load event accross browser without jquery

后端 未结 3 812
北海茫月
北海茫月 2020-12-12 02:24

I have to do the cross-domain ajax,so I try the jsonp.

Then I will have to add the script element dynamiclly to the page,however I can not check its sta

相关标签:
3条回答
  • 2020-12-12 03:02

    Looking at yepnope.js it uses both onload and onreadystatechange but it also uses a timeout loop to check the readystate property of the script element, see here.

    0 讨论(0)
  • 2020-12-12 03:08

    The load event for script elements was introduced in HTML5, prior to that it was only available for frameset and body elements (though most browsers also implemented it for img elements too). So if you want to support earlier browsers you'll need to be creative.

    One solution is to put a function call at the top of the script to say that the script has been loaded, as the browser should not execute any code in the script until it's loaded in its entirety.

    Another is to insert a second script with the "I'm loaded" function or flag immediately after the first in the presumption that the second will not be executed until the first has been loaded and executed.

    0 讨论(0)
  • 2020-12-12 03:15

    For IE, think of alternate ways to check load has happened, e.g. in your script file add the line

    script_myScriptUniqueName_loaded = true;
    

    and then in the dynamic add, if script.onload is not null (before setting a callback) use window.setInterval to check for existance of script_myScriptUniqueName_loaded, with a timeout to throw error, and if exists call callback.
    Remember to do window.clearInterval when you're done.

    0 讨论(0)
提交回复
热议问题