Iframe.readyState 在 chrome 中不起作用 [英] Iframe.readyState does not work in chrome
问题描述
我即时创建了一个 Iframe,并将下载二进制文件(xls、doc...)的页面设置为 url. 在下载文件时,我会显示一个动画.什么时候没有,我隐藏它.
I create an Iframe on the fly and set as the url a page that downloads a binary file (xls, doc...). While files are downloading I show an animation. When does not, I hide it.
问题是Chrome不知道文件什么时候完全下载,也就是iframe什么时候完全加载.我使用 iframe 属性 readyState
来检查 iframe 状态:
The problem is that Chrome does not know when the files are fully downloaded, that is when the iframe is completely loaded. I use the iframe property readyState
to check the iframe state:
var iframe = document.createElement("iframe");
iframe.style.visibility = "hidden";
// I start a progress animation
window.setTimeout(showProgressAnimation, 1000);
// I start the file download
iframe.src ='GetFile.aspx?file=' + fileName;
document.body.appendChild(iframe);
function showProgressAnimation() {
if (iframe.readyState == "complete" || iframe.readyState == "interactive") {
// I stop the animation and show the page
animation.style.display = 'none';
progressBar.hide();
$('#page').show();
}
else {
// Chrome is always getting into this line
window.setTimeout(showProgressAnimation, 1000);
}
}
所以结果是无限循环.
我已经尝试了以下方法,它在 Firefox 和 Chrome 中工作但不是当内容是二进制文件时:
I've tried the following and it works in Firefox and Chrome but not when the contents are a binary file:
if ($.browser.mozilla || $.browser.webkit ) {
iframe.onload = function showProgressAnimation() {
animation.style.display = 'none';
progressBar.hide();
$('#page').show();
}
}
// IE
else{
window.setTimeout(showProgressAnimation, 1000);
}
推荐答案
您可以使用 onload
来通知 iframe
You can use the onload
to signaling the load of the iframe
这是一个简单的例子
var iframe = document.createElement("iframe");
iframe.style.display = "none";
// this function will called when the iframe loaded
iframe.onload = function (){
iframe.style.display = "block";
alert("loaded");
};
// set the src last.
iframe.src ='http://www.test.com';
// add it to the page.
document.getElementById("one").appendChild(iframe);
在这里测试:
http://jsfiddle.net/48MQW/5/src
最后加载.
http://jsfiddle.net/48MQW/24/
Tested here:
http://jsfiddle.net/48MQW/5/
With src
loaded last.
http://jsfiddle.net/48MQW/24/
这篇关于Iframe.readyState 在 chrome 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!