javascript - Edge 浏览器 - iframe.document.open 不起作用

标签 javascript angularjs excel iframe microsoft-edge

我们有一些将数据导出到 Excel 文件的功能。 当点击“导出”按钮时,会调用一些客户端 JavaScript,首先检查客户端浏览器版本,并据此决定以哪种方式呈现 Excel 文档。 在本地测试时,它可以在 Chrome & Firefox & IE11 中运行。 但是,当我使用运行 Edge 浏览器的 Windows 10 计算机进行远程测试时,Excel 未呈现。

我可能会补充一点,我的本地计算机是 Win7 计算机,并且正在运行 VS2012 和 IE11。远程机器是Win10 with Edge,因此需要远程测试。 我已尝试在 IE11 F12 开发工具中进行模拟,但无法在那里复制 Edge 错误。

使用以下代码时,“open”会引发“未定义或空引用”错误:

excelIFrame.document.open("txt/html", "replace");
                    excelIFrame.document.write(sHTML);
                    excelIFrame.document.close();
                    excelIFrame.focus();
                    excelIFrame.document.execCommand("SaveAs", true, "Spreadsheet.xls");

iframe存在于html中,不是动态添加的。

<iframe id="excelIFrame" style="display:none"></iframe>

我尝试了以下可能的解决方案来使其正常工作,但无济于事 -

可能的解决方案 1: 将文档分配给临时变量时出现相同的“未定义或空引用错误”

var doc = excelIFrame.document;
                        doc.open("txt/html", "replace");
                        doc.write(sHTML);
                        doc.close();
                        doc.focus();
                        doc.execCommand("SaveAs", true, "Spreadsheet.xls");

可能的解决方案 2:使用 iFrame 的 contentWindow 属性。没有抛出错误,它只是打开不包含任何内容的“about:blank”。

excelIFrame.contentWindow.contents = sHTML;
                    excelIFrame.src = 'javascript:window["contents"]';

现阶段对此完全不知所措。 该页面是一个 angularJS 网页。 通过阅读它,我知道使用 iframe 时 document.open 在边缘存在问题。但以下链接document.open fails in an iframe我觉得可以解决问题。 非常感谢任何想法或建议。

最佳答案

这可能对正在搜索它的其他人有帮助。

    //For Edge browser ….. U have to write separate logic for each browser
    if (ua.match(/Edge/)){
       var blob = new Blob([sHTML], {type: 'data:application/vnd.ms-excel'});
       window.navigator.msSaveBlob(blob, "P2P_Report_"+new Date().getTime()+".xls");
    }

关于javascript - Edge 浏览器 - iframe.document.open 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199223/

相关文章:

excel - 如何在 VBA 中克隆范围对象

javascript - JS 将值推送到对象数组

javascript - 在 Karma 中动态加载 JavaScript 文件

asp.net - 如何使用 ExcelLibrary dll 卡住 Excel 中的标题行

javascript - 如果我在 Angular2 中创建新类,则会出现此错误 : TypeError: http_2. Headers is not a constructor

angularjs - 在 ng-repeat 中向 ng-click 函数添加参数似乎不起作用

excel - 根据其他列中的值使用循环添加/计算多个条件的行

javascript - 检查已启用按钮的数组并为每个按钮创建名称 "true"

javascript - Angular8 单选按钮检查功能意外运行

javascript - 动态添加特定属性到元素