问题描述
我动态创建的iframe并设置为URL的网页,下载一个二进制文件(XLS,DOC ...)。当文件被下载我展示的动画。当不呢,我隐藏它。
问题是,Chrome不知道什么时候该文件被完全下载,那就是当iframe是完全加载。我使用的iframe属性的readyState
来检查的iframe状态:
VAR IFRAME =使用document.createElement(IFRAME);
iframe.style.visibility =隐藏;
//我开始进度动画
window.setTimeout(showProgressAnimation,1000);
//我开始文件的下载
iframe.src ='?GetFile.aspx文件='+文件名;
document.body.appendChild(IFRAME);
功能showProgressAnimation(){
如果(iframe.readyState ==完成|| iframe.readyState ==互动){
//我停止动画,并显示在页面
animation.style.display =无;
progressBar.hide();
$('#页)显示()。
}
其他{
//浏览器总是要进入这一行
window.setTimeout(showProgressAnimation,1000);
}
}
因此,结果是一个无限循环。
我试过以下,它在Firefox和Chrome的工作,但不可以当内容是一个二进制文件:
如果($ .browser.mozilla || $ .browser.webkit){
iframe.onload =功能showProgressAnimation(){
animation.style.display =无;
progressBar.hide();
$('#页)显示()。
}
}
// IE
其他{
window.setTimeout(showProgressAnimation,1000);
}
您可以使用的onload
来信令 IFRAME
下面是一个简单的例子,工作
VAR IFRAME =使用document.createElement(IFRAME);
iframe.style.display =无;
//这个函数调用时iframe中加载
iframe.onload =功能(){
iframe.style.display =块;
警报(装);
};
//最后设置SRC。
iframe.src =HTTP://www.test.com';//把它添加到页面。
的document.getElementById(1)的appendChild(IFRAME)。
在这里测试:结果
结果
随着的src
最后加载。结果
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.
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);
}
}
So the result is an infinite loop.
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);
}
解决方案
You can use the
onload
to signaling the load of the iframe
here is a simple example that working
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);
Tested here:
http://jsfiddle.net/48MQW/5/
With src
loaded last.
http://jsfiddle.net/48MQW/24/
这篇关于Iframe.readyState不工作铬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!