我动态创建一个 Iframe 并将下载页面设置为 url二进制文件(xls、doc...)。下载文件时我会显示动画。当没有的时候,我就把它隐藏起来。
问题是 Chrome 不知道文件何时完全下载,即 iframe 何时完全加载。我使用 iframe 属性readyState
检查 iframe 状态:
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 中有效but not当内容是二进制文件时:
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
这是一个简单的例子
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/ http://jsfiddle.net/48MQW/5/
With src
最后加载。
http://jsfiddle.net/48MQW/24/ http://jsfiddle.net/48MQW/24/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)