如何在使用window.open时停止浏览器弹出窗口拦截器

问题描述:

我正在打印从后端检索到的html。如何在使用window.open时停止浏览器弹出窗口拦截器

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

这工作正常,在所有浏览器,只有我想不通的是如何阻止弹出式窗口拦截。 无法使用printWindow.location,因为html存在于变量中。

+0

你打开这个弹出窗口?用户操作或其他内容后? _“如果打开选项卡/弹出窗口的命令来自可信事件,则浏览器将只打开一个没有弹出窗口阻止程序警告的选项卡/弹出窗口。”_参见[此问题](http://*.com/questions/7139103/open-page-in-new-window-without-popup-blocking)获取更多详细信息。 – abhishekkannojia

+0

@abhishekkannojia我从一个按钮点击触发这个,打开浏览器打印窗口。 – PAVITRA

对于具有相同问题的人,
问题是我已经叫承诺在此方法中,当做到这一点, window.open是有导致的弹出窗口拦截器一个新的窗口实例。 这工作得很好我分配时,

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

到一个全局变量要在应许前和使用的方法内。