我们有一些将数据导出到 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/