Behzad Safamanesh
Behzad Safamanesh

Reputation: 39

Javascript: Issue with window.print

In recent update of browsers, (firefox 95+) Something weired happened.

Scenario:

- user clicks the print button in main page
- user will be redirected to a new page (print page)
- when the page fully loaded, the window.print() function executes
- when the print dialog dismissed, user will be redirected to main page

So for example, I wrote this html code:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
test
<script language="javascript">
    window.print();
    console.log('1');
</script>
</body>
</html>

As this page describes, The window.print() method will block while the print dialog is open. And its true. unless you have more script lines after that.

Opened an issue here and yet, the problem seems unsolved to me.

if you paste

window.print();
console.log('1');

directly into browser console, it works as it intended.

Any advise?

Upvotes: 1

Views: 943

Answers (1)

danh
danh

Reputation: 62676

This works for me in current Safari and Chrome. The console log is executed after the print dialog is dismissed.

window.onafterprint = event => {
  console.log('after print');
};
window.print();

Upvotes: 1

Related Questions