javascriptjquerytypescriptbrowsercross-browser

How to stop browser popup blocker when using window.open


I'm printing a html retrieved from back end.

 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.


Solution

  • 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. 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.