24

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!

oskar1983
  • 301
  • 1
  • 2
  • 9
  • 1
    Could this be due to network requests being open when you call window.print()? See https://code.google.com/p/chromium/issues/detail?id=285690 and/or http://stackoverflow.com/questions/14961769/javascript-window-print-intermittently-working-in-chrome for similar trouble. – natevw Dec 09 '13 at 20:07
  • I manage to fix this by adding `jquery.min.map` to my project folder. Check under the Network tab in Developer Tools for any pending file. Providing a proper path or removing it should solve the issue – rafaelcastrocouto Apr 01 '14 at 12:38

6 Answers6

15

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;
}
noypiscripter
  • 1,451
  • 1
  • 13
  • 13
  • Smart move stopping. Just curious, how did it occur to you that stopping the reload would work? Myself, I might have come up with reload and mark the problem as solved. – Martin Apr 14 '14 at 20:47
  • 1
    @Martin, simply reloading basically fixes the issue but once the Print dialog is closed, the page will reload. So I thought of stopping it from reloading using `window.stop`. – noypiscripter Apr 15 '14 at 23:46
7

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>
Steven Wilber
  • 405
  • 6
  • 10
  • This has been bugging me ages until I eventually found this answer. In my case this is also a better solution than the first answer as this only happens in debug mode in VS2013. My published site, thus when not running from VS, doesn't have the problem. – Neville Dec 10 '14 at 08:50
2

I have exactly same problem with Chrome. You need to manually reload page:

    <a href="javascript:window.print();window.location.reload()">Print</a> 
Tepliuk
  • 51
  • 5
2

If by any chance someone is using VS2013 with chrome, this problem is caused by the BrowserLink funcionality.

see SO answer here

Community
  • 1
  • 1
Francisco
  • 4,104
  • 3
  • 24
  • 27
1

Similar behavior in Safari. It is caused by opened HTTP request(s) on background.

When any HTTP request is in progress, window.print() is executed successfully, but no dialog is opened!

You will have this issue, when you use a long polling (for server push). Because client will have already opened HTTP connection for a long time, window.print() will never work.

Martin
  • 696
  • 6
  • 7
  • The "reload" workaround mentioned in the other answers works not because of the reload itself, but because before reloading the browser stops the ongoing requests. It can be proved by instead of reloading, clicking on the "X" button on the browser to stop requests in progress. – Fernando Basso Apr 11 '19 at 14:26
0

I am most certain you are experiencing this issue because you have a video element on your page - most probably an MP4.

If you disable this video / or have an OGV video instead, the printing should work fine. It is a bug in chrome itself due to limitations of Chrome's video implementation. It is also important to note that if the user prints manually with ctrl-p / cmd-p, print functions correctly

http://code.google.com/p/chromium/issues/detail?id=141633

Hope this helps :)

Wasiim
  • 146
  • 4
  • 16