Aay*_*ush 6 javascript javascript-events
我已经制作了一个js函数,用于在我的html中包含另一个javascript.我需要确保我使用该函数加载的脚本已完成处理,然后只在原始脚本中进一步移动.
function loadBackupScript(){
var script = document.createElement('script');
script.src = 'http://www.geoplugin.net/javascript.gp';
script.type = 'text/javascript';
var head = document.getElementsByTagName("head")[0];
head.appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)
我需要知道脚本已经加载以使用它的功能.我怎么做?
function loadBackupScript(callback) {
var script;
if (typeof callback !== 'function') {
throw new Error('Not a valid callback');
}
script = document.createElement('script');
script.onload = callback;
script.src = 'http://www.geoplugin.net/javascript.gp';
document.head.appendChild(script);
}
loadBackupScript(function() { alert('loaded'); });
Run Code Online (Sandbox Code Playgroud)