Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print preview freezes Javascript in Chrome

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?

like image 497
Tamás Sallai Avatar asked Dec 05 '16 10:12

Tamás Sallai


People also ask

Why does my Chrome freeze when I try to print?

Update Google Chrome Google Chrome crashes while you take a print from a web page might be due to an outdated browser build on your PC. While Google Chrome automatically updates to the latest version in the background, you can force the update manually.

Can I set the window print settings with Javascript?

The window. print() method calls the browser's build in print support plugin to print the current DOM page. you can check the documentation, it doesn't support any argument or settings. to setup the print, you can only utilize the browser's GUI( ex. enable or disable background graphics etc.)

How do I fix a print preview error?

Method 5: Reset low integrity level on the Low folder , click All Programs, and then click Accessories. Right-click Command Prompt, and then click Run as administrator. Restart the computer. Print or print preview a Web page in Internet Explorer.


1 Answers

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"');
like image 92
Balazs Zsoldos Avatar answered Oct 06 '22 03:10

Balazs Zsoldos