Created
June 19, 2014 08:28
-
-
Save yanhaijing/c5309bdd5a038ebadf57 to your computer and use it in GitHub Desktop.
异步载入js文件
This file contains hidden or 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
function importJs(jsurl, fCallback, fError){ | |
if (typeof(fCallback) != "function") fCallback = new Function(); | |
if (typeof(fError) != "function") fError = new Function(); | |
var oScriptEl, oTimeoutHDL, oHead; | |
oScriptEl = document.createElement("script"); | |
oScriptEl.type = "text/javascript"; | |
oScriptEl.language = "javascript"; | |
oScriptEl.src = jsurl; | |
oScriptEl.onreadystatechange = doCallback; | |
oScriptEl.onload = function() { | |
this.readyState = "complete"; | |
doCallback(); | |
}; | |
oTimeoutHDL = window.setTimeout(doError, 20000); | |
document.getElementsByTagName("head")[0].appendChild(oScriptEl); | |
function doCallback(){ | |
if (oScriptEl.readyState == "complete" || oScriptEl.readyState == "loaded") { | |
oScriptEl.onload = oScriptEl.onreadystatechange = new Function(); | |
fCallback(); | |
window.clearTimeout(oTimeoutHDL); | |
oScriptEl.parentNode.removeChild(oScriptEl); | |
} | |
}; | |
function doError(){ | |
oScriptEl.parentNode.removeChild(oScriptEl); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment