在IE11&中未定义script.readyState. FF [英] script.readyState is undefined in IE11 & FF
问题描述
我只是研究了一个动态脚本加载器,该脚本加载器在IE9中运行良好,但在IE11和FF中运行不正常.
I just worked on a dynamic script loader which works fine in IE9 but not in IE11 and not in FF.
这是我的代码:
function getResourceScript(filename)
{
var script = document.createElement('script');
script.setAttribute('src', mShuttlePath + "scripts/" + filename);
script.setAttribute('type', 'text/javascript');
script.setAttribute('language', 'javascript');
document.getElementsByTagName('head')[0].appendChild(script);
}
function documentLoadInit()
{
if (document.readyState == 'complete')
{
// check if all scripts are loaded
for (var n = 0; n < document.scripts.length; n++)
{
if (document.scripts[n].readyState != "complete" && document.scripts[n].readyState != "loaded")
{
setTimeout(documentLoadInit, 49);
return false;
}
}
Init();
}
else
{
setTimeout(documentLoadInit, 49);
}
}
getResourceScript("core/core.js");
getResourceScript("core/ajax.js");
这里的主要问题是,script.readyState已在IE11中删除-我发现了很多!
The main problem here is, that the script.readyState is dropped in IE11 - so much I found out!
但是如何更换呢?如何检查脚本何时加载/完成?
But how to replace it? How to check When the script is loaded / is finished?
有什么想法吗?
推荐答案
从MSDN:
注意:对于脚本元素,不再支持
readyState
.从Internet Explorer 11开始,使用onload
.有关信息,请参阅兼容性更改.
Note: For the script element,
readyState
is no longer supported. Starting with Internet Explorer 11, useonload
. For info, see Compatibility changes.
因此,除了检查readyState
外,您可以使用类似以下的内容:
So, instead of checking for the readyState
you can use something like this:
if (!script.addEventListener) {
//Old IE
script.attachEvent("onload", function(){
// script has loaded in IE 7 and 8 as well.
callBack();
});
}
else
{
script.addEventListener("load", function() {
// Script has loaded.
callBack();
});
}
此外,我非常确定您可以改善您的代码.并且setInterval()
在这种情况下更适合.阅读一些有关如何使用dom事件的信息,如果仍然存在兼容性问题,可以使用以下方法:
Also, I'm pretty sure that you can improve your code. And setInterval()
is more suitable in this case. Read a little bit about how to use dom events and if you still have compatibility issues, here is something that you could use:
function loadExtScript(src, test, callback) {
var s = document.createElement('script');
s.src = src;
document.body.appendChild(s);
var callbackTimer = setInterval(function() {
var call = false;
try {
call = test.call();
} catch (e) {}
if (call) {
clearInterval(callbackTimer);
callback.call();
}
}, 100);
}
该函数将测试作为参数.由于您是应用程序的设计师,因此您将知道什么是成功的测试.一旦此测试为真,它将执行回调.
The function takes a test as a parameter. Since you are the designer of the app, you’ll know what successful test is. Once this test is true, it will execute the callback.
这篇关于在IE11&中未定义script.readyState. FF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!