【发布时间】:2012-12-06 20:06:52
【问题描述】:
我动态创建一个 iframe,并将下载 二进制文件(xls、doc...)的页面设置为 url。 在下载文件时,我会显示一个动画。没有的时候,我把它藏起来。
问题在于 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 中运行但在内容是二进制文件时不能:
if ($.browser.mozilla || $.browser.webkit ) {
iframe.onload = function showProgressAnimation() {
animation.style.display = 'none';
progressBar.hide();
$('#page').show();
}
}
// IE
else{
window.setTimeout(showProgressAnimation, 1000);
}
【问题讨论】:
-
我知道这听起来很简单,但请尝试从代码中删除
if ($.browser.mozilla) {并重试。至少对我来说这是有效的。onload是我使用并使其工作的。 -
为什么不
$("iframe").on("load",function () { animation.style.display = 'none'; progressBar.hide(); $('#page').show(); });控制台有话要说? -
@Aristos 我做到了,但没有用。你试过铬吗?使用我现在拥有的完整跨浏览器代码更新了问题。
-
在 Chrome 上是的,我使用 chrome。你有什么错误吗?
-
您是否看到您使用
visible隐藏它,但您使用display显示它?这必须相同才能工作。在onload上发出警报以仔细检查未调用的情况。
标签: javascript jquery asp.net html iframe