搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

Learn More

Redirect via onafterprint working in Chromium but not Firefox

  • 1 回覆
  • 1 有這個問題
  • 9 次檢視
  • 最近回覆由 jscher2000

more options

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/

所有回覆 (1)

more options

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

有幫助嗎?

問個問題

如果您還沒有帳號,您必須先 登入您的帳號 來回覆文章。請 開始一個新問題