簡體   English   中英

將標記添加到頁面后檢測是否加載了javascript文件

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

我(使用javascript)是否可以檢測到javascript文件是否已經加載,如果沒有,則添加onload處理程序? 腳本標簽是由第3方庫動態添加的,因此我無法自己動態插入。

這是丑陋的解決方案,但可能:

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);

該函數每500毫秒(0.5秒)調用一次,如果我們有一些全局對象/功能,它將檢查3rdparty可能擁有的功能。 但是,如果3rdparty是由真正的gosu制造的,那么它就不可能有任何全局性的東西,因為所有工作都可能在某個匿名函數中進行,並且該解決方案將無法工作。

您應該說明要使用哪個3rdparty,以及何時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM