MS Edge - window.open 给出错误 "The operation was canceled by the user."

MS Edge - window.open gives error "The operation was canceled by the user."

我有两个不同的 window.open 函数调用都在 Microsoft Edge 中失败:

var canvas = document.getElementById("diagramCanvas");
var imgUrl = canvas.toDataURL("image/png");
window.open(imgUrl);

window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#results').html()));

它们都产生错误:

操作已被用户取消。

这看起来像一个弹出窗口拦截器,所以我禁用了 Edge 弹出窗口拦截功能,但两者仍然失败,同时确实会弹出带有真实网址的令人讨厌的弹出窗口:http://www.popuptest.com/

有没有办法让 Edge 在新的 window 中打开 javascript 生成的文件?

看起来问题可能是由于新 window 声明中 url 的长度所致。

我根据这个问题的答案找到了解决方法:

Download canvas to Image in IE using Javascript

来自回答:

Example code:

$("#save").click(function(){
    var html="<p>Right-click on image below and Save-Picture-As</p>";
    html+="<img src='"+canvas.toDataURL()+"' alt='from canvas'/>";
    var tab=window.open();
    tab.document.write(html);
});

通过打开一个新的 window 然后写入正文,可以避免 url 长度限制。

此格式适用于 IE9+ 和 Edge,以及 Chrome(并且应该适用于所有其他现代浏览器)。