javascript document.readyState 在 Firefox 和 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16689273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:38:42  来源:igfitidea点击:

document.readyState not working in Firefox and Chrome

javascriptjavascript-eventsdocumentdocument-ready

提问by Cyril

In my application, I am calling a method for every 1000msto check the document readyState. Following is the code which I am using,

在我的应用程序中,我每1000 毫秒调用一个方法来检查文档 readyState。以下是我正在使用的代码,

var success=setInterval(""CheckState()"",1000);

function CheckState(){

if($get('businessDownl').document.readyState=="interactive" || 
      $get('businessDownl').document.readyState=="complete"){
           alert("Great");
           clearInterval(success);
  } 
}

This code works fine in IE browser.But fails in Firefox and Chrome browser. I tried using $get('businessDownl').readyStatealso, it is printing as undefined. Can anybody tell me how to use the readyState for FF and chrome in the above scenario ?

此代码在 IE 浏览器中工作正常。但在 Firefox 和 Chrome 浏览器中失败。我$get('businessDownl').readyState也尝试使用 ,它打印为未定义。有人能告诉我如何在上述场景中为 FF 和 chrome 使用 readyState 吗?

回答by gkalpak

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 的文档,因此它是readyState,您需要访问 iframe 中的域(无论是否使用 jQuery)。
有关更多信息,请查看此处



You could do it using the iframe's contentWindowproperty (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()).

您可以使用 iframe 的contentWindow属性(不需要 jQuery)来完成。
请注意,为了访问 iframe 的document,您必须首先将元素添加到 DOM(例如使用window.document.appendChild())。

Sample code:

示例代码:

var businessDownl = document.createElement('iframe');
document.body.appendChild(businessDownl);
...
var state = businessDownl.contentWindow.document.readyState;

See, also, this short demo.
[Tested on latest versions of Firefox and Chrome.]

另请参阅此简短演示
[在最新版本的 Firefox 和 Chrome 上测试。]

(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).

(请注意,因为 iframe 加载速度很快,有时您只会看到“已完成”,有时会看到“正在加载”和“已完成”——有一次我什至有幸看到“未初始化”:D)。

回答by Rob Johnstone

If you just want to wait until the document is ready there is no need to keep checking - you can listen for the event:

如果您只想等到文档准备好,则无需继续检查 - 您可以监听事件:

var whenReady = function(callback) {
  if (document.readyState === 'complete') callback(); // check not already loaded prior to this function being called
  else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback); // for standards compliant browsers (including IE 9+)
  else if (document.attachEvent) document.attachEvent('onreadystatechange', callback); // for IE 8
};

whenReady(alert('loaded'));

The only downside of this technique is that it only supports IE 8 and later. Libraries such as JQuery offer better legacy browser support and a cleaner syntax:

这种技术的唯一缺点是它只支持 IE 8 及更高版本。诸如 JQuery 之类的库提供了更好的旧浏览器支持和更清晰的语法:

$(function() {
  // anything here will execute once the dom is ready
});