简体   繁体   English

如何在使用window.open时停止浏览器弹出窗口阻止程序

[英]How to stop browser popup blocker when using window.open

I'm printing a html retrieved from back end. 我正在打印从后端检索到的html。

 printHtml(htmlTemplate) {
    var printWindow = window.open('', '_blank');
    printWindow.document.write(htmlTemplate);
    setTimeout(function () {
        printWindow.document.close(); // necessary for IE >= 10
        printWindow.focus(); // necessary for IE >= 10*/
        printWindow.print();
        printWindow.close();
    }, 1000);
}

This works fine in all browsers and only thing I can't figure out is how to stop the pop up blocker. 这适用于所有浏览器,只有我无法弄清楚的是如何阻止弹出窗口阻止程序。 Can't use printWindow.location because the html is there in a variable. 不能使用printWindow.location,因为html存在于变量中。

For anyone having the same issue, 对于任何有同样问题的人,
Issue was I have called this method inside a promise and when that is done, window.open is having a new window instance resulting the popup blocker. 问题是我在promise中调用了这个方法,当完成后,window.open有一个新窗口实例导致弹出窗口阻止程序。 This worked fine when I assigned, 当我分配时,这很好用,

var printWindow = window.open('', '_blank');

to a global variable before going in to the promise and used that inside the method. 在进入promise之前使用全局变量并在方法中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM