IE在<a href=""> click</a>上打开空白窗口

时间:2014-03-05 21:25:31

标签: html html5 asp.net-mvc-3

我在视图中有一个链接:

<a href="@ViewBag.ReportHelpLink" target="_blank">@ViewBag.ReportHelpText</a>

其中@ViewBag.ReportHelpLink会生成Excel文档的链接。如

http://www.website.com/SomeDocument.xls

注意,指定了target="_blank"。我刚才添加了这个,以确保当用户单击此链接时,IE不会将当前页面更改为空白。但是,当这样做时,IE会打开一个新的空白窗口和一个文件的下载框。

有没有办法在没有额外空白窗口的情况下弹出IE中的下载框,或者当前页面没有变为空白?

与往常一样,其他浏览器也不错。只有IE想要以某种方式打开空白页面。

1 个答案:

答案 0 :(得分:2)

创建iframe并将其用作下载目标。见Download File Using Javascript/jQuery

function downloadURL(url) {
    var hiddenIFrameID = 'hiddenDownloader',
        iframe = document.getElementById(hiddenIFrameID);
    if (iframe === null) {
        iframe = document.createElement('iframe');
        iframe.id = hiddenIFrameID;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
    }
    iframe.src = url;
};

如果您只想要HTML,just set the target of the link to be an iframe

<a href="https://www.swiftview.com/tech/letterlegal5.doc" target="ifr">Download</a>
<iframe name='ifr'></iframe>