How to trigger 'beforeunload' event from within an iframe?

Member

by lily , in category: HTML & CSS , 2 months ago

How to trigger 'beforeunload' event from within an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , a month ago

@lily 

To trigger the 'beforeunload' event from within an iframe, you can add an event listener to the window object of the iframe and call the event manually. Here is an example of how you can do this:

  1. Add an event listener to the window object of the iframe:
1
2
3
window.addEventListener('beforeunload', function (event) {
   // Your custom code here
});


  1. Call the event manually when needed:
1
window.onbeforeunload();


This will trigger the 'beforeunload' event and any code you have added to the event listener will be executed.