How to raise onunload event when removing iframe?

How can I fire an iframe unload event when it is deleted?

Using the following code, when removeChild (iframe) is called, the onunload event does not fire.

<!-- parent.html --> <html> <head> <title>remove iframe</title> <script type="text/javascript"> window.onload = function() { var button = document.getElementsByTagName("BUTTON")[0]; button.onclick = function() { document.body.removeChild(document.getElementsByTagName("IFRAME")[0]); }; }; </script> </head> <body> <iframe src="./son.html" /> <button>Remove iframe</botton> </body> </html> <!-- son.html --> <html> <head> <title>son html</title> <script type="text/javascript"> window.onload = function() { alert("hello"); }; window.onunload = function() { alert("world!"); }; </script> </head> <body style="background-color:#aaccee;"> </body> </html> 

How can I call him?

+4
source share
1 answer

Before deleting a frame, you can set the src attribute for the iframe to "about: blank", check to see if this iframe event is triggered by the onload event.

EG:

 var iframe = document.getElementsByTagName("IFRAME")[0]; iframe.src = "about:blank"; document.body.removeChild(iframe); 
+8
source

Source: https://habr.com/ru/post/1388565/


All Articles