How to call javascript function of parent window when child is closed?

I am creating a popup. After I finished working on the child (pop-up) window and click the close button, I need to call the javascript function of the parent window. How can i achieve this. I myself do not create a child window, but display the contents of some other url.

+4
source share
3 answers

I do not think that you can receive an event because you cannot communicate with the document itself when the URL belongs to another domain. However, you can poll and check the "closed" property of the window object:

var w = window.open("http://what.ever.com", "OtherWindow"); setTimeout(function() { if (w.closed) { // code that you want to run when window closes } else setTimeout(arguments.callee, 100); }, 100); 

You can also start the interval timer if you want:

 var w = window.open("http://what.ever.com", "OtherWindow"); var interval = setInterval(function() { if (w.closed) { // do stuff cancelInterval(interval); } }, 100); 
+9
source

If the child window does not come from the same domain name as the parent window, you are blocked due to the same origin policy . This is intentional to prevent cross-site scripting (XSS) attacks.

0
source

Do not vote for it. This is just an improvement on Pointy code to get rid of arguments.callee . Vote for Pointy.

 var w = window.open("http://what.ever.com", "OtherWindow"); setTimeout(function timeout() { if (w.closed) { // code that you want to run when window closes } else setTimeout(timeout, 100); }, 100); 
0
source

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


All Articles