Created
November 27, 2014 10:07
-
-
Save steel1990/f2d53375a60213259a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = document; | |
var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; | |
var baseElement = head.getElementsByTagName("base")[0]; | |
var currentlyAddingScript; | |
var loadJS = function (url, callback, charset) { | |
var node = doc.createElement("script"); | |
charset = charset || 'utf-8'; | |
node.charset = charset; | |
var onload = function (error) { | |
// Ensure only run once and handle memory leak in IE | |
node.onload = node.onerror = node.onreadystatechange = null; | |
// Remove the script to reduce memory leak | |
head.removeChild(node); | |
// Dereference the node | |
node = null; | |
callback(error); | |
}; | |
if ('onload' in node) { | |
node.onload = onload; | |
node.onerror = function() { | |
onload(true); | |
}; | |
} else { | |
node.onreadystatechange = function() { | |
if (/loaded|complete/.test(node.readyState)) { | |
onload(); | |
} | |
}; | |
} | |
node.async = true; | |
node.src = url; | |
// For some cache cases in IE 6-8, the script executes IMMEDIATELY after | |
// the end of the insert execution, so use `currentlyAddingScript` to | |
// hold current node, for deriving url in `define` call | |
currentlyAddingScript = node; | |
// ref: #185 & http://dev.jquery.com/ticket/2709 | |
baseElement ? head.insertBefore(node, baseElement) : head.appendChild(node); | |
currentlyAddingScript = null; | |
}; | |
var loadJSList = function (urls, callback) { | |
var url = urls.splice(0, 1)[0]; | |
if (!url) { | |
callback(); | |
} else { | |
loadJS(url, function () { | |
Ali.loadJS(urls, callback); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment