【问题标题】:MS Edge - window.open gives error "The operation was canceled by the user."MS Edge - window.open 给出错误“操作已被用户取消”。
【发布时间】:2017-06-17 02:30:49
【问题描述】:

我有两个不同的 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 弹出窗口阻止,但仍然失败,而带有真实 URL 的令人讨厌的弹出窗口确实会弹出:http://www.popuptest.com/

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

【问题讨论】:

    标签: javascript microsoft-edge


    【解决方案1】:

    看起来问题可能是由于新窗口声明中 url 的长度。

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

    Download canvas to Image in IE using Javascript

    来自答案:

    示例代码:

    $("#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);
    });
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多