Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome: window.print() print dialogue opens only after page reload (javascript)

Tags:

I am facing a really weird problem. I am calling window.print() from a javascript file. This is working fine in Safari, IE, Firefox... and until two hours ago it worked in Chrome, too. (Version 29.0.1547.57)

I did not change anything essential in my javascript file (really - I just removed some comments...), but what now happens is really weird: In Chrome, the print dialogue does not open when window.print() is called. Nothing happens. But then, when I press reload, the print dialogue opens immediately.

The behaviour in the other browser did not change. And while debugging in Chrome I can see that window.print() is called as expected and the script goes on after that. Only the print dialogue is not shown until pressing reload.

Has anybody ever experienced something like that? I also tried to call window.print() in setTimeout(), but this did not change anything. When I debug the content of the page which shall be printed appears to be perfectly loaded.

I am sorry to ask, but I did not find anything while researching. Any help would be appreciated!

Thank you!

like image 754
oskar1983 Avatar asked Sep 04 '13 19:09

oskar1983


People also ask

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.)

What does Window print () do in JavaScript?

print() Opens the print dialog to print the current document. If the document is still loading when this function is called, then the document will finish loading before opening the print dialog.

How do I get to the print dialog in Chrome?

There are two ways to get to the system print dialog from Chrome. If you've already pressed the Ctrl+P keyboard shortcut, then look for the 'Print using system dialog' option at the very bottom of the left column. What is this? To jump directly to the system print dialog, you can use the Ctrl+Shift+P keyboard shortcut.


2 Answers

Wasiim is right, there is a Chrome bug where window.print() does not work when there is a <video> tag in the DOM. I solved it by calling this function:

function printPage() {     window.print();      //workaround for Chrome bug - https://code.google.com/p/chromium/issues/detail?id=141633     if (window.stop) {         location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear         window.stop(); //immediately stop reloading     }     return false; } 
like image 90
noypiscripter Avatar answered Sep 18 '22 12:09

noypiscripter


From my experience this is due to continued background traffic, e.g. ajax calls and the like that prevent Chrome from feeling the that page is loaded completely. The reload breaks all traffic and thus the print dialog pops up. This is a particular gotcha in Visual Studio 2013 where BrowserLink continually ticks away in the background. This can be tested by disabling BrowserLink via the setting below:

<configuration>     <appSettings>         <add key="vs:EnableBrowserLink" value="false"/>     </appSettings> </configuration> 
like image 40
Steven Wilber Avatar answered Sep 21 '22 12:09

Steven Wilber