Open popup and refresh parent page when popup closes

I opened the window.open popup in JavaScript, I want to refresh the parent page when I close this popup. (onclose event?) How can I do this?

window.open("foo.html","windowName", "width=200,height=200,scrollbars=no"); 
+72
javascript html javascript-events
May 29 '12 at 2:23
source share
11 answers

You can access the parent window using window.opener ', so in the child window write the following:

 <script> window.onunload = refreshParent; function refreshParent() { window.opener.location.reload(); } </script> 
+173
May 29 '12 at 2:35 a.m.
source share

There is no close event in the popup that you can listen to.

On the other hand, there is a private property that is set to true when the window closes.

You can set a timer to check this private property and do it like this:

 var win = window.open('foo.html', 'windowName',"width=200,height=200,scrollbars=no"); var timer = setInterval(function() { if(win.closed) { clearInterval(timer); alert('closed'); } }, 1000); 

See this working example Scripts !

+25
May 29 '12 at 2:31
source share

on your child page, put:

 <script type="text/javascript"> function refreshAndClose() { window.opener.location.reload(true); window.close(); } </script> 

and

 <body onbeforeunload="refreshAndClose();"> 

but as a good user interface design, you should use the Close button because it is more user friendly. see code below.

 <script type="text/javascript"> $(document).ready(function () { $('#btn').click(function () { window.opener.location.reload(true); window.close(); }); }); </script> <input type='button' id='btn' value='Close' /> 
+10
May 29 '12 at 2:38 a.m.
source share

window.open will return a link to the newly created window, provided that the open URL matches the Same source policy .

This should work:

 function windowClose() { window.location.reload(); } var foo = window.open("foo.html","windowName", "width=200,height=200,scrollbars=no"); foo.onbeforeunload= windowClose;​ 
+3
May 29 '12 at 2:42
source share

I use this:

 <script language='javascript'> var t; function doLoad() { t = setTimeout("window.close()",1000); } </script> <script type="text/javascript"> function refreshAndClose() { window.opener.location.reload(true); window.close(); } </script> <body onbeforeunload="refreshAndClose();" onLoad='doLoad()''> 

when the window closes it, then updates the parent window.

+3
Oct 02 '13 at 3:26
source share

If your application runs in an HTML5-enabled browser. You can use postMessage . The above example is very similar to yours.

+1
May 29 '12 at 2:39 a.m.
source share

In my case, I opened a popup when I clicked on a link on the parent page. To update a parent when closing a child using

 window.opener.location.reload(); 

in the child window is called to reopen the child window (possibly due to the viewing state, I think. Correct me if I am wrong). Therefore, I decided not to reload the page in the parent and load the page, again assigning it the same url.

To avoid the popup popup after closing the popup, this may help,

 window.onunload = function(){ window.opener.location = window.opener.location;}; 
+1
Jun 27 '15 at 13:02
source share

Try

  self.opener.location.reload(); 

Open the parent of the current window and reload it.

+1
Sep 16 '15 at 10:41
source share

You can get to the main page using the parent command (parent window) after the step, which you can do everything ...

  function funcx() { var result = confirm('bla bla bla.!'); if(result) //parent.location.assign("http://localhost:58250/Ekocc/" + document.getElementById('hdnLink').value + ""); parent.location.assign("http://blabla.com/" + document.getElementById('hdnLink').value + ""); } 
0
Oct. 15 '14 at 9:51
source share

You can use the code below on the parent page.

 <script> window.onunload = refreshParent; function refreshParent() { window.opener.location.reload(); } </script> 
0
Nov 03 '14 at 4:37
source share

The following code will handle the post post post update:

 function ManageQB_PopUp() { $(document).ready(function () { window.close(); }); window.onunload = function () { var win = window.opener; if (!win.closed) { window.opener.location.reload(); } }; } 
0
03 Oct '16 at 9:44
source share



All Articles