Print preview of freezes Javascript in Chrome

I struggled with the fact that Chrome removes Websocket connections if print preview is open for more than a few seconds. I traced it to this ticket , and the reason is that window.print() is synchronous and thus stops any other script execution, which in turn cause the network timeout and drop.

Since the ticket was opened 2.5 years ago, and is currently WontFix, I am looking for a workaround.

What i tried

Use window.open

I originally used an iframe to render content and print. Then I tried to move it to open a new tab, load the contents and print it.

window.open("iframe.html");

You can find a minimal example here (code here ). If you open the console, you will see its count every second. After 2 seconds, a pop-up window will open (you may have to enable pop-ups), which loads the print in a new tab. Wait a few seconds, then close the preview. If you return to the original tab, you will see that the counter is stopped.

Use binding

Then I tried to use the anchor tag with target="_blank" , for example:

<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 a link and use Open link in new tab , then the counter works.


Are there other ways to open a window that uses a different execution context? Or any ideas for another workaround?

+5
source share
1 answer

A workaround has been found if the problem is that the problem 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 a noopener . If you don’t need to open the original window from javascript just opened, and blocking JS in a new window is not a problem, adding this attribute works in Chrome.

+1
source

Source: https://habr.com/ru/post/1260812/


All Articles