SirMissAlot
SirMissAlot

Reputation: 130

Problem: Jquery printing close window immediately after open

In some Chrome browsers when the user click on the print button immediately close.

My code was :

newWin = window.open("");
newWin.document.write("some content");
newWin.print();
newWin.close();

I'v tried:

newWin.focus();
newWin.print();
newWin.close();

And:

setTimeout(function () { newWin.print(); }, 500);
newWin.onfocus = function () { setTimeout(function () { newWin.close(); }, 500); }

And:

var document_focus = false; // var we use to monitor document focused status.

// Now our event handlers.
$(document).ready(function() { newWin.window.print();document_focus = true; });
setInterval(function() { if (document_focus === true) { newWin.window.close(); }  }, 300);

But non of them worked and the print window still closes.

How can i prevent the print window closes?

Thanks.

Upvotes: 0

Views: 297

Answers (1)

SirMissAlot
SirMissAlot

Reputation: 130

I've worked around it with iFrame

The iFrame:

 <iframe id="print_this_iframe" hidden></iframe>

The Jquery:

var dstFrame = document.getElementById('print_this_iframe');
var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
dstDoc.write('Some Content');
dstDoc.close();
dstFrame.onload = function() { $("#print_this_iframe").get(0).contentWindow.print()}; 

Upvotes: 1

Related Questions