2012-11-01 36 views
2

我們的代碼管理器今天停機,導致我們的網站爬到了一片喧譁。由於標籤管理器中的代碼不是關鍵任務(分析等),是否有辦法執行以下操作?是否有跨瀏覽器的方式來設置第三方(跨域)異步Dom注入腳本超時?

var extScript = document.createElement('script'); 
extScript.type = 'text/javascript'; 
extScript.src = 'http://third-party.com/scriptfile.js'; 
var s = document.getElementsByTagName('script')[0]; 
s.parentNode.insertBefore(extScript, s); 

window.setTimeout(function() { 
    // if script not loaded - "give up" 
}, 3000); // 3 secs 
+0

你能只需插入幾秒鐘後腳本給一切時間先加載? –

+0

是的,當然你可以設置超時時間然後「放棄」。但是你想放棄什麼? – Bergi

+1

我認爲他正在尋找一種方法來中止腳本,以便瀏覽器停止「旋轉」。因此,我也在尋找一種方法來做到這一點,但沒有提出太多。 – JavaKungFu

回答

1

如果它不是關鍵任務,你可能會因爲文件完全加載想盡快把這個腳本在body標籤的結束。

window.onload = function() { 
    var extScript = document.createElement('script'); 
    extScript.type = 'text/javascript'; 
    extScript.src = 'http://third-party.com/scriptfile.js'; 

    document.body.appendChild(extScript); 

} 
0

加入async屬性如何?

<script async type = 'text/javascript' src = 'http://third-party.com/scriptfile.js' ></script> 

這不會阻止,但你可能會遇到一些問題(like this one

相關問題