我在blackberry9300(5.0.0.832)。我试图以编程方式添加一个javascript元素。
var script=document.createElement('script');
script.src='file.js';
script.type='text/javascript';
script.onload=function(){alert('loaded');}; //Nothing happens
script.addEventListener('load', function(){alert('loaded');}, false); //Nothing happens
script.addEventListener('readystatechange', function(){alert('state changed');}, false); //Nothing happens
script.onreadystatechange=function(){alert('state changed')}; //Nothing happens
document.getElementsByTagName('head').item(0).appendChild(script);
有没有办法解决这个问题?
答案 0 :(得分:3)
function loadScript(path,callback,chkvar){
var a=document.createElement('script');
a.src=path;
a.type='text/javascript';
a.onreadystatechange=function(){
if(this.readyState=='complete'||this.readyState=='loaded')
callback();
};
a.onload=callback;
if(a.hasOwnProperty('onload')==false)
var e=setInterval(function(){
if(eval(chkvar)){ //chkvar will only be available after js file is loaded.
clearInterval(e);
callback();
}
},50);
}
document.getElementsByTagName('head').item(0).appendChild(a);
}