script . , . :.
if (window.localStorage) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = ;
document.body.appendChild(script);
}
else {
setTimeout(doTheNextThing, 10);
}
function doTheNextThing() {
}
... script localStorage stuff doTheNextThing — , localStorage, script doTheNextThing, , , . , ( setTimeout) : , , (, , , , IE).
. , script, , . , ( window, window.jQuery), - ():
function loadAndWait(src, name, timeout, callback) {
var stop, script;
if (window[name]) {
setTimeout(function() {
callback("preexisting");
}, 10);
}
else {
script = document.createElement('script');
script.type = "text/javascript";
script.src = src;
document.body.appendChild(script);
stop = new Date().getTime() + timeout;
setTimeout(poll, 150);
}
function poll() {
if (window[name]) {
callback("loaded");
}
else if (new Date().getTime() > stop) {
callback("timeout");
}
else {
setTimeout(poll, 75);
}
}
}
... jQuery:
loadAndWait(
"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
"jQuery",
10000, // ten seconds or whatever
function(result) {
// ...do the next one if result !== "timeout"
}
);
loadAndWait , :
loadThese(
[
{ src: "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
symbol: "jQuery"
},
{
src: "http://the-next-one",
symbol: "nextSymbol"
}
],
doTheNextThing
);
function loadThese(scripts, callback) {
var index = 0;
run("okay");
function run(result) {
var entry;
if (result === "timeout") {
callback(result);
}
else if (index < scripts.length) {
entry = scripts[index++];
loadAndWait(entry.src, entry.symbol, 10000, run);
}
else {
callback("loaded");
}
}
}
loadThese run script .
, , , .
, : , , ? , , . script , . 50k ? , , ... , (, !), ...
. , , localStorage. , CDN, script ( , ), . . () CDN , , . . " HTTP" YUI ( "", ). .