How to detect window.print() finish for iframe?

Member

by denis , in category: HTML & CSS , a month ago

How to detect window.print() finish for iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , a month ago

@denis 

You can detect when the print dialogue box has been closed by adding an event listener to the beforeprint and afterprint events.


Here is an example of how you can detect when the printing process has finished for an iframe:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var iframe = document.getElementById('myIframe');

iframe.contentWindow.onbeforeprint = function() {
  console.log('Print dialog opened');
};

iframe.contentWindow.onafterprint = function() {
  console.log('Print dialog closed');
  // Add your code here to handle completion of printing
};


In this example, we are adding event listeners to the onbeforeprint and onafterprint events of the iframe's content window. When the print dialog is opened, the onbeforeprint event will be triggered and a message will be logged to the console. When the print dialog is closed, the onafterprint event will be triggered and a message will be logged to the console.


You can add your code to handle the completion of printing inside the onafterprint event listener function.