这里我有一个问题。下面我动态创建脚本元素。问题是 ga.onload=function() 不能在 ie 中工作。其他浏览器如 ff、chrome 都正常工作。但 IE 不能工作。代码是
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = "../../script/jquery.hint.js";
ga.onload = function(){
some operation here;
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
最佳答案
您可以让它适用于所有浏览器。问题是 IE 报告状态不可靠,它并不总是报告完成,有时它只是报告加载。以下代码对我来说工作正常。
runFun = "";
var newScript = document.createElement('script');
if(document.all){
newScript.onreadystatechange = function() {
if (newScript.readyState == 'complete') {
newScript.onreadystatechange = "";
runFun = new Function("alert('here');");
}
else if (newScript.readyState == 'loaded') {
newScript.onreadystatechange = "";
runFun = new Function("alert('here');");
}
runFun();
}
}
else {
// most browsers
newScript.onload = function() {
var runFun = new Function("alert('here');");
runFun();
}
}
关于javascript - 使用javascript如何克服ie版本的document.createElement ('script' .onload问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4506875/