Skip to content

Instantly share code, notes, and snippets.

@yanhaijing
Created June 19, 2014 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanhaijing/c5309bdd5a038ebadf57 to your computer and use it in GitHub Desktop.
Save yanhaijing/c5309bdd5a038ebadf57 to your computer and use it in GitHub Desktop.
异步载入js文件
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