Last active
December 18, 2015 04:39
-
-
Save RockyMyx/5726472 to your computer and use it in GitHub Desktop.
JavaScript: load-async.js
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
/** | |
* From: http://coolshell.cn/articles/9749.html | |
**/ | |
function cachejs(script_filename){ | |
var cache = document.createElement('object'); | |
cache.data = script_filename; | |
cache.id = "coolshell_script_cache_id"; | |
cache.width = 0; | |
cache.height = 0; | |
document.body.appendChild(cache); | |
} | |
function loadjs(script_filename) { | |
var script = document.createElement('script'); | |
script.setAttribute('type', 'text/javascript'); | |
script.setAttribute('src', script_filename); | |
script.setAttribute('id', 'coolshell_script_id'); | |
script_id = document.getElementById('coolshell_script_id'); | |
if(script_id){ | |
document.getElementsByTagName('head')[0].removeChild(script_id); | |
} | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
function LoadJS(){ | |
var script = './alert.js'; | |
loadjs(script); | |
} | |
cachejs('./alert.js'); | |
<div class="btn" onclick="LoadJS()">Click to load alert.js </div> | |
// From: http://bbs.html5cn.org/thread-1177-1-8.html | |
function loadScript(url, callback) { | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
//IE | |
if (script.readyState) { | |
script.onreadystatechange = function () { | |
if (script.readyState == "loaded" || script.readyState == "complete") { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
} | |
} else { | |
//非IE | |
script.onload = function () { | |
callback(); | |
} | |
} | |
script.src = url; | |
document.getElementById("head").appendChild(script); | |
} | |
//第一步加载jquery类库 | |
loadScript("jquery/jquery-1.4.1.js", function () { | |
//第二步加载hello.js | |
loadScript("js/hello.js", function () { | |
//第三步加载world.js | |
loadScript("js/world.js", function () { | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment