19

The print method does not work on window when using an onclick link Safari. What is the alternative to have a webpage print, in safari via onclick code that is placed on a button? Another odd behavior that's occurring is that when I try and close the window, the print dialog native to the browser then appears.

Rodrigo5244
  • 5,145
  • 2
  • 25
  • 36
fauverism
  • 1,970
  • 3
  • 33
  • 59

6 Answers6

6

Try this solution:

try {
  // Print for Safari browser
  document.execCommand('print', false, null)
} catch {
  window.print()
}
Alex Golovin
  • 232
  • 3
  • 6
  • This try catch statement did not run for me but the `document.execCommand` does work in Safari. I rewrote this to check for the browser and conditionally run each statement. – rosalynnas Jul 31 '20 at 20:30
  • This will silently fail for Firefox 94 without throwing any error. – Drew Nov 22 '21 at 17:54
  • [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) is deprecated as of today. – kissu Apr 25 '22 at 02:36
  • (2022 July 26): Does not work. 50% opens op the small white print window which then results in an empty document – DavidDunham Jul 26 '22 at 08:01
2

I was facing a similar issue with the Safari browser (and not any others). In my case, aborting all API calls currently in progress immidiately showed the native print dialog.

I'm guessing the reason why your dialog showed up when trying to close the tab/page is that all the network requests are cancelled then.

Here's an example of how to create a pool of ajax requests (when using jQuery): Stop all active ajax requests in jQuery

Also, to make it work consistently, I had to wrap the $.xhrPool.abortAll function in a short timeout (100ms).

My click handler function (simplified) looks something like this:

function myFunction() {
    window.print();
    window.setTimeout(function () {
        $.xhrPool.abortAll()
    }, 100);
}
ReGdYN
  • 536
  • 2
  • 7
1

If you return the result of window.print() to the onclick handler, Safari will not display the print window. I use React and had to make the following change:

//code that does not work:
<button type="button" onClick={() => window.print()}>Print!</button>

//code that does work:
<button type="button" onClick={() => { window.print(); }}>Print!</button>

All other browsers I tested (except Safari on iOS) worked fine either way, such as Chrome on iOS or Chrome for desktop.

My best guess is that as window.print is known to be asynchronous in Safari, perhaps window.print() returns falsy to the onclick handler, which cancels any "popups" from being displayed. This may be why the sample above with the myFunction() code may work when having onclick="window.print()" may not work.

1

This currently works in Firefox, Chrome and Safari:

try {
  if(!document.execCommand('print', false, null)) {
    window.print()
  }
} catch {
  window.print()
}
0
            var frame1 = document.createElement('iframe');
            frame1.name = "frame3";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var mywindow = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.documentElement ? frame1.contentDocument.documentElement : frame1.contentDocument;
            mywindow.document.open();
            mywindow.document.write('<html><head><title>' + strTitle + '</title>');
            mywindow.document.write('</head><body>');
            mywindow.document.write(data);
            mywindow.document.write('</body></html>');
            mywindow.document.close(); // necessary for IE >= 10
            setTimeout(function () {
                window.frames["frame3"].focus(); // necessary for IE >= 10
                window.frames["frame3"].print(); // necessary for IE >= 10
            }, 500);
            setTimeout(function () {
               document.body.removeChild(frame1);
            }, 20000);
            
        
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 14 '22 at 17:52
-2

Have no problem with window.print() with Safari. Sorry I could not try on Windows OS but follow code works for me with MacOS:

<!DOCTYPE html>
<html>
<body>
  <button onclick="myFunction()">Click me</button>
  <a href="#" onclick="myFunction()">Click link</a>

  <script>
    function myFunction() {
      window.print();
    }
  </script>

</body>
</html>