javascript - 如何防止 execCommand ('SaveAs' 打开新选项卡或窗口

标签 javascript dom internet-explorer-9 domdocument execcommand

我必须在 IE 中生成下载弹出窗口。 我正在使用下面的代码。 当我单击按钮时,它会打开一个新选项卡和“另存为对话框”

 function SaveContents(element) {

            if (document.execCommand) {
                var oWin = window.open("about:blank","_blank");![enter image description here][1]
                oWin.document.write(element);
                oWin.document.close();
                var success = oWin.document.execCommand('SaveAs', false, "FilteredReport.xls")
                oWin.close();

            }

}

如何在不打开新窗口或选项卡的情况下显示“另存为”对话框..

我也可以在不使用的情况下将字符串数据写入Excel

oWin.document.write(element);

因为它被写入打开的新选项卡或窗口

下图解释了..

enter image description here

最佳答案

使用下面的代码片段代替 IEwindow.document.execCommand('SaveAs', true, fileName + ".xls");

if (window.navigator.msSaveOrOpenBlob) {
     blobObject = new Blob([CSV]);
     window.navigator.msSaveOrOpenBlob(blobObject, 'Export.xls');
 }
IEwindow.close();

关于javascript - 如何防止 execCommand ('SaveAs' 打开新选项卡或窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24120637/

相关文章:

javascript - 在 Three.js CSSRenderer 中补偿浏览器缩放

javascript - 正则表达式排除不符合由分隔符分隔的两个模式之一的匹配项

javascript - 以编程方式添加 html 后,Jquery .click 不起作用

javascript - 如何在 IE 9 中使用 JavaScript 从样式属性获取背景颜色作为十六进制值?

javascript - 如何在 Angular Firebase 中向前台发送后台通知以在 Angular 9 webapp 中显示通知?

javascript - Angular 获取共享指令的所有元素的列表

javascript - 在 Angular 1.4.4 中,为什么范围变量的更改会在没有 $watch 或 $apply 的情况下传播到 DOM?

javascript - 为什么这在 JS 中不起作用?? getElementById().style.width += 40 + "px";

css - IE9中的子导航向上推

asp.net - IE9 将网站扔到 Quirks 模式