Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...