下载文件时在Chrome中检测iframe的onload

时间:2014-09-18 12:13:26

标签: html google-chrome iframe onload

我在HTML页面中使用隐藏的iframe进行下载。我想知道它什么时候完成。我已经在互联网上搜索了很多,每个人都说我应该使用iframe的onload事件处理程序,但它在Chrome中不适合我!

我不知道在哪里,但我读到的地方是因为我用iframe而不是HTML文档下载文件。我必须承认它是Chrome特有的,它在FireFox中运行良好。

我还测试了readstateonreadystatechange但没有成功。

我创造了一个演示问题的小提琴:



function download(url)
{
    var iframe = document.createElement("iframe");
    iframe.src = url;
    iframe.onload = function () {
        console.log('Download completed.');
    };
    document.body.appendChild(iframe);
}

<button onclick="download('http://www.colorado.edu/conflict/peace/download/peace.zip');">Download</button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function downloadURL(url) {
    var hiddenIFrameID = 'hiddenDownloader',
        iframe = document.getElementById(hiddenIFrameID);
    if (iframe === null) {
        iframe = document.createElement('iframe');
        iframe.id = hiddenIFrameID;
        iframe.style.display = 'none';
        iframe.onload = function () {
            alert('Download completed.');
        };
        document.body.appendChild(iframe);
    }
    iframe.src = url;
}
&#13;
<button onclick="downloadURL('http://www.colorado.edu/conflict/peace/download/peace.zip');">Download</button>
&#13;
&#13;
&#13;

相关问题