Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.print() from Chrome subwindow - close, then breaks

When in our webapp a Chrome subwindow is launched, either by window.open() or by user clicking a link with target="_blank", then in that subwindow, body onload="window.print()" to auto-launch the print dialog and print preview, then user CLOSES the print/subwindow instead of clicking cancel, the parent window gets completely hosed. Specifically:

  1. No javascript events will fire
  2. No links are clickable
  3. Hitting F5 shows the little spinner in the tab but the page never reloads.
  4. The parent window is truly dead -- all you can do is close it.

If you click cancel on the subwindow (where the print-preview is launched via window.print()) everything is fine. But if user closes the window, all the craziness happens.

This is a known bug in Chrome:

  • bug 1
  • bug 2
  • bug 3

Does anyone know of a workaround for this?

like image 765
HerrimanCoder Avatar asked May 20 '14 05:05

HerrimanCoder


1 Answers

Instead of using window.print(), just bring your content to new window and then call print functionality as given below, to print the content.

following is a function call where we passed element's inner html through it's id to new window.

PrintContent(document.getElementById('div-content-id').innerHTML);

function PrintContent(printableContent) {
    var printWindow = window.open("", "Print_Content", 'scrollbars=1,width=900,height=900top=' + (screen.height - 700) / 2 + ',left=' + (screen.width - 700) / 2);
    printWindow.document.write(printableContent);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
    return false;
}

I was also facing same problem, it works for me.

like image 98
V2Solutions - MS Team Avatar answered Oct 23 '22 10:10

V2Solutions - MS Team