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

Member

by lily , in category: HTML & CSS , a year ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , a year 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.