开发者

Detect if javascript file is loaded after the tag has been added to the page

开发者 https://www.devze.com 2023-03-03 18:38 出处:网络
Can I (using javascript) detect if a javascript file has already loaded, and if not, add an onload handler? The script tag is a开发者_Python百科dded dynamically by a 3rd party library so I\'m not able

Can I (using javascript) detect if a javascript file has already loaded, and if not, add an onload handler? The script tag is a开发者_Python百科dded dynamically by a 3rd party library so I'm not able to dynamically insert it myself.


This is ugly solution, but possible:

function thirdPartyLoaded() {
  // here we will have code, that will be executed,
  // when 3rd party script will be loaded
}

// Also we know, that 3rd party have some global object or function
// Let it be IAm3dPartyFunc for example

// Then we somewhere add continuos watcher function with setInterval
var watchFor3rd = setInterval(function(){
    if (undefined != IAm3dPartyFunc) {
        thirdPartyLoaded();
        clearInterval(watchFor3rd);
    }
}, 500);

This function will be called each 500ms (0.5 second) and will check, if we have some global object/function, that 3rdparty could have. But if 3rdparty is made by the true gosu, then it couldn`t have any global stuff, as all work could be in some anonymous function and this solution will not work.

You should state, which 3rdparty you want to use and when.

0

精彩评论

暂无评论...
验证码 换一张
取 消