-1

I want have an entry form (HTML5, CSS3, Chrome Version 65.0.3325.181) and want the data printed. I call up a special site where the print is prepared. At the very end of it I have

    <script type="text/javascript">
        window.print(); 
        window.location.href="aktien.php";
    </script>
</body> 

When I leave out the line with window.location I get the print dialogue and can print the page, but of course it stops afterwards.

If I don't omit that line, the system goes directly to aktien.php, without print dialogue and without printing.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

0

That's because the browser asks the operating system to take over when .print() is hit and then moves on to the next line, which it handles itself. It gets to the next line before the OS has presented the print dialog and moves you to the new page, where a whole new page takes over.

You can delay the call to move to the new page until after the print dialog has been presented with a timer:

<script type="text/javascript">
    window.print(); 
    // Defer the call to change the location just long enough 
    // for the OS to get the print dialog up on the screen. At that
    // point, the UI will become blocked and nothing will happen until
    // the dialog is cleared. Here, we're delaying by just 20 milliseconds.
    setTimeout(function(){
      window.location.href="aktien.php";
    }, 20);
</script>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Thanks both of you I tried both versions on Chrome and Firefox. - both versions work perfectly in both browsers - If you cancel the print dialogue/the printing: in both versions & browsers the system continued correctly, in my exaplme with aktien.php Your answers were a great halp, thanks a lot – Christian Apr 05 '18 at 14:36
  • @Christian Don't forget to up vote all helpful answers and to mark the one that solved your problem as "the" answer. – Scott Marcus Apr 05 '18 at 14:37
0

Maybe try this:

    <script type="text/javascript">
      window.onafterprint = function() {
        window.location.href="aktien.php";
      }

      window.print();
    </script>
</body> 

The window.onafterprint event is supposed to trigger when printing is done.

No, I have not tested it. But this should do what you want. It should wait until the print is over and then redirect to the new page.

I do not know what will happen if you cancel the print dialog, or if the print fails. But this should be a start.

Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • `onafterprint` is not supported in all browsers. – Scott Marcus Apr 03 '18 at 22:13
  • True, Opera and Safari do not seem to support it. But the original question did specify a version of Chrome. – Intervalia Apr 03 '18 at 22:15
  • Yes, but being that it is non-standard, there's no guarantee that it will continue to be supported in browsers that currently do. – Scott Marcus Apr 03 '18 at 22:16
  • Thanks both of you I tried both versions on Chrome and Firefox. - both versions work perfectly in both browsers - If you cancel the print dialogue/the printing: in both versions & browsers the system continued correctly, in my exaplme with aktien.php Your answers were a great halp, thanks a lot – Christian Apr 05 '18 at 14:37