How to know when popup is closed in javascript
This code will open go.com in popup window:
<a href="javascript:void(0)" onClick="javascript:window.open('http://go.com','','status=yes,scrollbars=yes,toolbar=no,menubar=no,location=no ,width=300px,height=300px')">open</a>
and I want to use alert('closed!')
in main page, when popup is closed and I can't edit popup page for use onunload="alert('closed!')"
, because the popup page is an external page and I can't change the code like
var mywindow = window.open开发者_如何学JAVA('http://go.com') ...
Thanks.
How about instead of directly loading that external page in the window, you open up a "local" window with an iframe containing the external page content? That way you still have enough control over the actual window to be able to tell when it's closed.
Some drawbacks: the address bar will not show the URL they are browsing and it may also break if go.com has some sort of frame busting code.
Some example code:
function openIFrameInWindow(url) {
var myWindow = window.open("");
var iframe = document.createElement("iframe");
iframe.src = url;
myWindow.document.body.appendChild(iframe);
myWindow.document.body.onbeforeunload = function () { alert("WINDOW CLOSED") };
}
<a href="javascript:void(0)" onClick="javascript:openIFrameInWindow('http://go.com')">open</a>
How about checking the status of the window every 1 second to see if it has been closed? Sample code, not tested:
var mywindow= window.open();
checkmywindow();
function checkmywindow()
{
if(mywindow.closed)
{
//code when popup is closed
}
else
{
setTimeout("checkmywindow();", 1000);
}
}
When the popup window is closed you can INFORM the opener window about it by using the window.opener property.
So:
//pseudocode in pop-up window
onClose() {
window.opener.popUpClosed = true;
}
Now in your page that opened the popUp you can check for the value of the popUpClosed variable.
More on the opener property here: http://www.webreference.com/js/tutorial1/opener.html
精彩评论