I've got a web page with a button that I want to print the page and then redirect to another page. However, using Firefox 66, if I go through with the print, it doesn't redirect. If I cancel the print, it redirects just fine.

Here's the code:

<SCRIPT LANGUAGE="JavaScript">
  function myprint() {
    window.print();
    window.location='mypage.php';
  }
</script>

<a><button onClick="myprint(); return false;">Print ticket</button></a>

If I remove the window.print(); line, it works fine, but when actually printing, it does not. Also works on Chrome, Edge, and even IE11, but not Firefox.

Am I missing something?

Update: It's not just my computer. Other peoples' computers do the same on Firefox...

1 Answers

0
Community On Best Solutions

First of all, your code should work, but a (way) better version is:

<SCRIPT LANGUAGE="JavaScript">
  function myprint() {
    window.onafterprint = function(event) {
        window.location.href = 'index.php'
    };

    window.print();
  }
</script>

<a><button onClick="myprint(); return false;">Print ticket</button></a>

Where you create a function wich should trigger after the print is done. This event (window.onafterprint) is supported by every browser except safari (EDIT: yep, from MDN)

Now, for some reason, this code don't work with Firefox 66.0.3 64bit (in my case), but work flawlessly with IE11 and Chrome 73.

Well, actually it work if you cancel the print when asked, but not if you actually print.

A work around is to use the other event, onbeforeprint, which change the page before printing (the idea is to remove things like ads before printing and then use onafterprint to restore the page). When i tryed, it seems to print the first page and not the second, so this event is broken too.

This way seems to work.

<SCRIPT LANGUAGE="JavaScript">
  function myprint() {
    window.onbeforeprint = function(event) {
        window.location.href = 'index.php'
    };

    window.print();
  }
</script>

<a><button onClick="myprint(); return false;">Print ticket</button></a>