Redirect via onafterprint working in Chromium but not Firefox
I am trying to serve a print view and redirect the user to the page they came from right after printing (or cancelling). This works fine in Chromium with the following bit of JavaScript code:
function printPageAndRedirectTo(redirectUrl) { window.addEventListener("afterprint", function () { window.location.href = redirectUrl; }); window.print();
In Firefox, however, nothing happens at all. Debugging shows that the callback is called, but the href field remains unchanged.
This is mysterious to me. Any ideas what the reason might be?
Firefox version is 84.0.2, Chromium 87.0.4280.88 (both from the Arch Linux repositories).
Edit:
Here's a fiddle to illustrate. Redirect to Wikipedia works in Chromium but not in Firefox: https://jsfiddle.net/xnazLb6p/1/
Edit 2:
Another one to show that the problem here is redirecting from the event listener, not redirecting itself: https://jsfiddle.net/xnazLb6p/2/
All Replies (1)
Hmm, I added
console.log('Calling location.href...');
as the first line of your event handler function. The text prints to the console right away as the Print dialog appears, rather than once printing is completed. I suspect that having the Print dialog still up locks the page and prevents the navigation, but that's just a guess.
This seems to be the relevant bug, but the comments are confusing about whether it was fixed in Firefox 51 or not: https://bugzilla.mozilla.org/show_bug.cgi?id=1214805