您需要添加代码
- 插入需要的脚本代码,然后
- 使用定时器间隔反复LY检查来自进口脚本
这里对象的示例代码:
var addScript=function(filename,callback){
var e=document.createElement('script');
e.type = 'text/javascript';
e.src = filename;
if(callback){
e.onloadDone=false;//for Opera
e.onload=function(){e.onloadDone=true;callback();};
e.onReadystatechange=function(){
if(e.readyState==='loaded'&& !e.onloadDone){
e.onloadDone=true;callback();
}
}
}
if(typeof(e)!=='undefined'){
document.getElementsByTagName('head')[0].appendChild(e);
}
}
addScript('http://yoursite.com/js/yourScript.js',function(){functionFromYourScript();});
(当然你会想:
function writeTags(){
//write the script tags
}
function check(){
// example for prototype library
if(window.Prototype && Prototype.Version){
doActualWork();
}else{
window.setTimeout(check, 200);
}
}
function doActualWork(){
// this your actual code that requires
// the loaded library
}
writeTags();
check();
你可能正确地附加了脚本标记。我怀疑问题在于你必须等待浏览器在该函数调用可以工作之前加载脚本。 – Pointy 2010-10-10 21:14:20