document.readyState无法在Firefox和Chrome中运行

Cyr*_*ril 2 javascript document javascript-events document-ready

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

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

function CheckState(){

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

此代码在IE浏览器中工作正常.但在Firefox和Chrome浏览器中失败.我$get('businessDownl').readyState也尝试使用 ,它是打印为undefined.有谁能告诉我如何在上面的场景中使用readyState for FF和chrome?

gka*_*pak 5

注意:为了能够访问iframe的文档,因此readyState,您需要访问iframe中的域(无论使用jQuery如何).
有关详细信息,请查看此处.


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

示例代码:

var businessDownl = document.createElement('iframe');
document.body.appendChild(businessDownl);
...
var state = businessDownl.contentWindow.document.readyState;
Run Code Online (Sandbox Code Playgroud)

另见这个简短的演示.
[测试了最新版本的Firefox和Chrome.]

(请注意,因为iframe加载速度很快,有时你会看到"完成",有时"加载"和"完成" - 一旦我甚至幸运地看到"未初始化":D).

  • 这对我不起作用.浏览器提示我下载iframe下载文件大约需要10秒钟.但是对于IE,Chrome或Firefox来说,`iframe.contentWindow.document.readyState`立即是"完整的". (3认同)