Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for child window closing?

I am opening a child window for Facebook sharing this way:

window.open(sharingUrl,'','toolbar=0,status=0,width=626,height=436'); 

The window is automatically closed when a user clicks share or close...

Is there a way to add a listener to these events?

like image 435
Oleg Tarasenko Avatar asked Apr 19 '11 05:04

Oleg Tarasenko


People also ask

How do I know if my child window is closed?

If you store a reference to the child window when you call window. open() , then you can poll using setInterval() to see whether the window is still open using the window. closed property.

How do I close the child window from the parent window?

A child window opens as separate window as a tab. Go back to the parent and click “Close child” and the child window closes. Viola!

What is window opener?

opener. The Window interface's opener property returns a reference to the window that opened the window, either with open() , or by navigating a link with a target attribute. In other words, if window A opens window B , B. opener returns A .

How do you check if a window is closed in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.


2 Answers

If you store a reference to the child window when you call window.open(), then you can poll using setInterval() to see whether the window is still open using the window.closed property. The example below checks twice per second.

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436'); var timer = setInterval(checkChild, 500);  function checkChild() {     if (child.closed) {         alert("Child window closed");            clearInterval(timer);     } } 

Note to others: If you are ever in a situation where you have control over the html in the child window, you could make use of the onbeforeunload event and alert the parent window.

like image 146
jessegavin Avatar answered Oct 21 '22 11:10

jessegavin


For future references, I'd like to share another solution that doesn't require setInterval :

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436'); child.onunload = function(){ console.log('Child window closed'); }; 
like image 20
Odubuc Avatar answered Oct 21 '22 13:10

Odubuc