Open Popup and Refresh Parent Page on Close Popup

Open popup and refresh parent page on close popup

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

<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>

Refresh Parent page on close of pop up window

Method 1

<script>
function popup() {
var win = window.open("", "Page Title", "toolbar=no, location=no");
win.document.body.innerHTML = '<a href="#" onclick="window.opener.location.reload();window.close();">Close Me</a>';
}
</script>
<a href="#" onclick="popup()">Open Me</a>

It creates a popup with a link to close the window and refresh parent.

Demo: https://jsfiddle.net/eke4f72r/

Method 2

<script>
function popup() {
var win = window.open("", "Page Title", "toolbar=no, location=no");
var win_timer = setInterval(function() {
if(win.closed) {
window.location.reload();
clearInterval(win_timer);
}
}, 100);
}
</script>
<a href="#" onclick="popup()">Open Me</a>

It detects from the parent window if child is closed. If true, it reloads the page.

Demo: https://jsfiddle.net/gv6nmdn9/

EDIT When using method 1, make your parent to open the popup you want and just add this in your child:

<a href="#" onclick="window.opener.location.reload();window.close();">Close Me</a>

Refreshing Parent window after closing popup

Subscribe to the unload event in the child window and call the parent window from the child window to notify it is closing!

Edit Added a code sample...

function popupClosing() {
alert('About to refresh');
window.location.href = window.location.href;
}

var w = window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
w.onunload = function () {
window.parent.popupClosing()
};

How to refresh parent page after closing popup window in javascript?

As from my comment from the other answer you just need to handle the window.onunload event and use the window.opener property to tell the calling page to be refreshed.

2.add.jsp

<html>
<head>
<script type="text/javascript">

//ADDED START
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
//ADDED END

$(document).ready(function(){
$("#savebtn").click(function(e) {
$.ajax({
type: "POST",
url: "RecordHandler",
data: dataString,
success: function(data){
$('body').html(data);
$('#msg').html('New Record Added Successfully.');
window.timeout(CloseMe, 1000); <-- not sure on the syntax
but have a timeout which triggers an event
to close the form once a success has been handled.
Probably should do something incase of an error.
}
});

return false; <-- this should stop the window from unloading.
});

function CloseMe()
{
window.opener.location.reload();
window.close();
}
</head>

How to reload parent page on closing PopUp window?

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

onunload="window.opener.location.reload();"

or use this

<script>
window.onunload = refreshParent;
function refreshParent() {
var retVal = confirm("Are you sure ?");
if( retVal == true ){
window.opener.location.reload();
else
return false;

}
</script>

Can I refresh my parent window when closing the popup?

The only way I know is by checking child window status at frequent intervals but this is explained clearly in this question

That said, I don't know if that could be an alternative, but using a lightbox instead of a new window popup would allow you to keep full control on your events as the whole thing stays in the same window.

Most lightbox API's offer that kind of functionality (loading an external site in the lightbox instead of the usual image), using dynamically generated iFrame to display the external site. This solution also have drawbacks (e.g.: frame-busting code on site loaded in lightbox) but can look nicer than a plain old new window...

I've been using Shadowbox on projects for quite some time now and always liked it, but there are plenty of others out there, maybe even better.

Auto refresh parent window after closing popup

Try putting this javascript code in your popup window:

window.onunload = function(){
window.opener.location.reload();
};


Related Topics



Leave a reply



Submit