2011-03-25 74 views
3

我正在做一個動態的腳本調用,以在窗口的onload事件之前加載一些extenal JS。這裏是示例代碼。Javascript:在Firefox中的Onload事件沒有觸發

var temp = document.createElement('script'); 
temp.type = 'text/javascript'; 
temp.async = temp.defer= true; 
temp.src = "http://stevesouders.com/efws/iframe-empty.php?t=="; 
window.setTimeout(function(){document.getElementsByTagName('head')[0].appendChild(temp)},0); 

此代碼完全適用於IE(Window onload事件不等待動態腳本資源下載)。但它不適用於Firefox。 Window onload事件正在等待動態腳本資源下載。

是否存在針對此問題的任何解決方法。您可以使用http://nidhisekhar.com/samples/async_script_call_settimeout.html鏈接查看IE和Firefox上的行爲。我感謝您的幫助。

感謝, 拉賈

回答

0

試試這個JavaScript函數:

調用示例:

addScriptToHead(路徑, '', 'JS');