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

Member

by denis , in category: HTML & CSS , 7 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 7 months 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.

Related Threads:

How to detect whether an iframe is loaded?
How to detect when iframe scripts are executed?
How to detect when the browser blocks an iframe?
How to detect if browser support iframe?
How to detect an error 404 in an iframe?
How to detect close in another domain of iframe?