我正在使用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);
有什么办法可以解决此问题?
解决方法:
较早的操作系统(< 6.0)上的blackberry浏览器不会触发onload事件,也不会更改脚本的readyState.因此,我需要轮询js文件是否成功加载.如果有人感兴趣,这里就是该功能.
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);
}