Check if a popup window is closed

I am opening a popup window with

var popup = window.open('...', '...');

This javascript is defined in a control. This control is then used from a web page. I want to reload the page which opens this popup when the popup is closed.

Basically user is required to input some denominations in the popup window and submit. These denominations are then stored in user sessions. And when user clicks submit I am closing the popup window and at the same time want to refresh the window which opens this popup to refetch the updates which user made in the popup.

I am trying to do

var popup = window.open('...','...');
if (popup) {
  popup.onClose = function () { popup.opener.location.reload(); }
}

I guess I am doing it wrong coz this isn’t seems to be working.

For testing the issue I’ve even tried this but no alert appeared.

if (popup) {
  popup.onclose = function() { 
    alert("1.InsideHandler");
    if (opener && !opener.closed) { 
      alert("2.Executed.");
      opener.location.reload(true); 
    } else { 
      alert("3.NotExecuted.");
    }
  }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here’s what I suggest.

in the popup you should have:

<script type="text/javascript">

function reloadOpener() {
  if (top.opener && !top.opener.closed) {
    try {
      opener.location.reload(1); 
    }
    catch(e) {
    }
    window.close();
  }
}
window.onunload=function() {
  reloadOpener();
}
</script>
<form action="..." target="hiddenFrame">
</form>
<iframe style="width:10px; height:10px; display:none" name="hiddenFrame" src="about:blank"></iframe>

then in the server process you can return

<script>
   top.close();
</script>

Old suggestions

There is no variable called popup in the popup window.
try

var popup = window.open('...','...');
if (popup) {
  popup.onclose = function () { opener.location.reload(); }
}

or with a test:

popup.onclose = function () { if (opener && !opener.closed) opener.location.reload(); }

PS: onclose is not supported by all browsers

PPS: location.reload takes a boolean, add true if you want to not load from cache
as in opener.location.reload(1);

Method 2

Try this:

window.opener.location.reload(true);

or

Into the popup before closing

window.opener.location.href = window.opener.location.href;
window.close();

Method 3

Ok so what you do is as follows.

create a method “setWindowObjectInParent” then call that in the popup.
var popupwindow;

function setWindowObjectInParent(obj)
{
popupwindow = obj;
}

Now you have an object that you can call focus on.

So in the popup add

$(window).unload(function(){
window.opener.setWindowObjectInParent();
});
window.opener.setWindowObjectInParent(window);

This will unset the obj when the popup is closed and set the object when it is opened.

Thus you can check if your popup is defined and then call focus.

Method 4

Instead you should use a modal dialog like so

var popup = window.showModalDialog("page.html", "", params);

This would stop your at this line until the dialog is closed.

Somewhere in your page.html you would then code some javascript to set the window.returnValue which is what will be place the the popup variable.

Update

Window.showModalDialog() is now obsolete.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x