Reputation: 23500
Can I (using javascript) detect if a javascript file has already loaded, and if not, add an onload handler? The script tag is added dynamically by a 3rd party library so I'm not able to dynamically insert it myself.
Upvotes: 0
Views: 723
Reputation: 4215
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.
Upvotes: 1