Loading Javascript file on daemond by using document.createElement() and Preload Hacks-- The orginal article is Javascript Async Loading
The Javascript file is alert.js
The test source code as below:
<script language="javascript" type="text/javascript">
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);
}
</script>
...
<p style="cursor: pointer" onclick="LoadJS()">Click to load alert.js </p>
...
...