admin管理员组

文章数量:1320594

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>

(Example here, code here)

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?

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>

(Example here, code here)

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?

Share Improve this question asked Dec 5, 2016 at 10:25 Tamás SallaiTamás Sallai 3,3651 gold badge17 silver badges26 bronze badges 1
  • 2 Just ran into exactly the same issue! – Chris Nevill Commented Mar 6, 2017 at 17:04
Add a ment  | 

1 Answer 1

Reset to default 7

Found workaround if the issue is that the issues is that the print preview of the newly opened window/tab blocks the main tab:

<a href="xxxx" target="blank_" rel="noopener" />.

The trick is the noopener. If you do not have to access the original window from the javascript of the newly opened one, and blocking JS on the new window is not a problem, adding this attribute works in Chrome.

If opening a window in javascript you can use

window.open('your_url_here', 'Page Title Here', 'rel="noopener"');

本文标签: Print preview freezes Javascript in ChromeStack Overflow