gpt4 book ai didi

javascript - Iframe.readyState 不适用于 chrome

转载 作者:IT王子 更新时间:2023-10-29 03:20:48 27 4
gpt4 key购买 nike

我即时创建了一个 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);
}

最佳答案

您可以使用 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/
最后加载 src
http://jsfiddle.net/48MQW/24/

关于javascript - Iframe.readyState 不适用于 chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com