document.readyState无法在Firefox和Chrome中运行
在我的应用程序中,我为每个 1000ms 调用一个方法来检查文档readyState。以下是我正在使用的代码,
In my application, I am calling a method for every 1000ms to check the document readyState. Following is the code which I am using,
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
,它打印为未定义。有谁能告诉我如何在上面的场景中使用readyState for FF和chrome?
This code works fine in IE browser.But fails in Firefox and Chrome browser. I tried using
$get('businessDownl').readyState
also, it is printing as undefined. Can anybody tell me how to use the readyState for FF and chrome in the above scenario ?
注意:为了能够访问iframe的文档,因此它是 readyState ,你需要访问iframe中的域(无论使用jQuery)。
欲了解更多信息,请查看这里。
NOTE: In order to be able to access the document of an iframe and thus it's readyState
, you need to have access to the domain in the iframe (regadless of the use of jQuery).
For more info, take a look here.
您可以使用iframe的 contentWindow
属性(不需要jQuery)来实现。
请注意,要访问iframe的文档
,您必须先将元素添加到DOM中(例如,使用 window.document.appendChild()
)。
You could do it using the iframe's contentWindow
property (no jQuery required).
Note that, in order to access the iframe's document
, you have to add the element to the DOM first (e.g. using window.document.appendChild()
).
示例代码:
var businessDownl = document.createElement('iframe');
document.body.appendChild(businessDownl);
...
var state = businessDownl.contentWindow.document.readyState;
另请参阅 简短演示 。
[测试最新版本的Firefox和Chrome。]
See, also, this short demo.
[Tested on latest versions of Firefox and Chrome.]
(请注意,因为iframe加载速度很快,有时你只看到已完成,有时加载和已完成 - 一旦我甚至幸运地看到未初始化:D)。
(Notice that, because the iframe loads quickly, sometimes you see only "completed", sometimes "loading" and "completed" - once I was even lucky enough to see "uninitialized" too :D).