I've been struggling with Chrome dropping the Websocket connections if a print preview is open for more than a few seconds. I've traced it back to this ticket, and the cause is that window.print()
is synchronous and thus halts any other script execution, which in turn make the websocket timeout and drop.
Since the ticket was opened 2.5 years back and it's currently WontFix, I'm looking for a workaround.
What I've tried
Use window.open
Originally, I used an Iframe to render the content and print it. Then I've tried to move it to open a new tab, load the contents there and print it.
window.open("iframe.html");
You can find a minimal example here (code here). If you open the console, you see it's counting up every second. After 2 secs, a popup will open (you might need to enable popups) that loads the print in a new tab. Wait a few seconds, then close the print preview. If you switch back to the original tab, you can see that the counter was stopped.
Use an anchor
Then I've tried using an anchor tag, with target="_blank"
, like this:
<a href="iframe.html" target="_blank">print</a>
This opens a new tab, but the counter still stops. If I right click on the link and use Open link in new tab
, then the counter works.
Are there and other ways to open a window that uses a different execution context? Or any ideas for a different workaround?