在我的应用程序中,我每隔 1000 毫秒 调用一个方法来检查文档 readyState
。以下是我正在使用的代码:
var success=setInterval(""CheckState()"",1000);
function CheckState(){
if($get('businessDownl').document.readyState=="interactive" ||
$get('businessDownl').document.readyState=="complete"){
alert("Great");
clearInterval(success);
}
}
此代码在 IE 浏览器中运行良好,但在 Firefox 和 Chrome 浏览器中运行失败。我尝试使用
$get('businessDownl').readyState
同样,它打印为未定义。谁能告诉我如何在上述情况下为 Firefox 和 Chrome 使用 readyState
?
最佳答案
注意:为了能够访问 iframe 的文档并因此它是 readyState
,您需要访问 iframe 中的域(无论是使用 jQuery)。
有关更多信息,请查看 here .
您可以使用 iframe 的 contentWindow
属性来实现(不需要 jQuery)。
请注意,为了访问 iframe 的 document
,您必须先将元素添加到 DOM(例如使用 window.document.appendChild()
)。
示例代码:
var businessDownl = document.createElement('iframe');
document.body.appendChild(businessDownl);
...
var state = businessDownl.contentWindow.document.readyState;
另请参阅此 short demo .
[在最新版本的 Firefox 和 Chrome 上测试。]
(请注意,由于 iframe 加载速度很快,有时您只会看到“已完成”,有时会看到“正在加载”和“已完成”——有一次我什至幸运地看到了“未初始化”:D)。
关于javascript - document.readyState 在 Firefox 和 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16689273/