HerrimanCoder
HerrimanCoder

Reputation: 7228

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:

Does anyone know of a workaround for this?

Upvotes: 10

Views: 2581

Answers (1)

V2Solutions - MS Team
V2Solutions - MS Team

Reputation: 1127

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.

Upvotes: 1

Related Questions