Edge浏览器- iframe.document.open不工作

Edge Browser - iframe.document.open not working

本文关键字:open 工作 document iframe 浏览器 Edge      更新时间:2023-09-26

我们有一些将数据导出到excel文件的功能。当"导出"按钮被点击时,一些客户端javascript被调用,首先检查客户端浏览器版本,并以此为基础,决定以哪种方式呈现excel文档。它是在Chrome &火狐浏览器,本地测试时的IE11。但是,当我使用运行Edge浏览器的windows 10机器远程测试时,excel没有呈现。

我可能会补充说,我的本地机器是Win7机器,我运行VS2012和IE11。远程机器是带有Edge的Win10,因此需要进行远程测试。我已经在IE11 F12开发工具中尝试了仿真,但无法复制Edge错误。

使用以下代码时,'open'抛出'未定义或null引用'错误:

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:在将文档分配给临时变量时出现相同的"未定义或null引用错误"

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网页。通过阅读,我了解到这份文件。当使用iframes时,在边缘打开是有问题的。但下面的链接文档。open在一个我认为可以解决问题的框架中失败。如有任何意见或建议,不胜感激。

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

    //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");
    }