這是可能的如果有一個符號可以在每個腳本中測試,或者每個腳本可以在完成加載時執行函數調用。
加載腳本很容易,因爲我敢肯定你知道(你會看到人們追加到head
代替;沒有關係,和body
是很容易找到)
var script = document.createElement('script');
script.src = /* ... the source path ... */;
document.body.appendChild(script);
但困難的部分是知道它何時被下載和執行。唯一真正的方法是通過輪詢來查看由腳本定義的新全局符號是否已經定義,或者在腳本完成加載時腳本會主動回覆(a'la JSONP)。
無論哪種方式,一旦您檢測到符號或獲得回調,然後您繼續加載下一個腳本。
這裏是做找到一個全球性的符號(在window
對象上的屬性)的基礎上的快速和骯髒的草圖:
// The scripts to load
var scriptList = {
scripts: [
{
file : 'some-script.js',
callback: function() {
// do some code here
},
symbol : "someSymbol",
timeout : 30000 // We know this one is slow, give it 30 seconds
},
{
file : 'another-script.js',
callback: function() {
// do some code here
},
symbol : "anotherSymbol"
},
// ...
]
};
// Triggering the load
loadScripts(scriptList);
// Library routines to do the load
function loadScripts(list)
{
var index, timeout;
// Start with the first one (loadNextScript starts with an increment)
index = -1;
loadNextScript();
// This function loads the next script in the list; if there are no
// more, it simply returns
function loadNextScript()
{
var script;
// Are there more?
++index;
if (index < list.length)
{
// Yes, append a `script` element
script = document.createElement('script');
script.src = list.file;
document.body.appendChild(script);
// Determine when to time out
timeout = new Date() + (list[index].timeout || 20000); // Time out in Xms, default 20 seconds
// Start polling
setTimeout(pollForScript, 0); // Async, but almost immediately (4-10ms on most browsers)
}
}
// This function polls to see if the current script has loaded yet by
// checking for a global symbol it defines.
function pollForScript()
{
var result;
// Has it been too long?
if (new Date() > timeout)
{
// Yes
result = "timeout";
}
else
{
// Has the symbol been defined?
if (typeof window[list[index].symbol] !== "undefined")
{
// Yes
result = "loaded";
}
else
{
// Nope, keep waiting
setTimeout(pollForScript, 250); // Check every quarter-second
}
}
// Did we get a result?
if (result)
{
// Yes, do the callback telling it of the result
try {
list[index].callback(result);
}
catch (e) {
}
// Load the next script
loadNextScript();
}
}
}
**題外話**:在你的對象文本的最後一個屬性之後,那些晃來晃去逗號將是你的問題在一些瀏覽器,細節:http://blog.niftysnippets.org/2010 /09/literal-improvement.html – 2010-11-22 11:00:47
我放置了這個逗號,表示將會有超過這些腳本被加載,無論如何感謝您的評論,也爲帖子,但我不認爲放置超時是一件好事的想法,因爲我打算做一些普遍適用的東西,不管它包含什麼回調代碼.. – 2010-11-22 14:32:25