Posted by: prajapatinilesh | May 17, 2009

How to refresh the parent window on closing the child window ?


Demo Example:

1) parent.php

<head> <title>Parent Window</title>
<script language=”javascript”> = ‘parentWindow’;
var newPopupWindow;
function popupLinkOption(url,width,height) {,”childWindow”,”height=”+height+”,width=”+width+”,left=100,top=100,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no”);
Parent File <br />
<a href=”#” onclick=”popupLinkOption(‘child.php’,200,200);”> Popup </a>
if ($_GET[‘rId’]) {
echo “<br />Rendom Id –>”.$_GET[‘rId’];


2) child.php

<head> <title>Child Window</title>
<script language=”javascript”>
function refreshParent() {
//here window.opener is the parent window from which child window opened.
parentURL = window.opener.location.href;
if (parentURL && parentURL != ‘undefined’) {
//alert(window.opener.location.href + ‘ — ‘ + window.opener.location);
parentURLArr = parentURL.split(‘#’);

//find last characters from the string means # as we have href=’#’ in parent window link
//newStr = parentURL.substring(0, parentURL.length-1);

parentURLArr1 = parentURLArr[0].split(‘?rId=’);

qStr = ‘?rId=’+Math.random(); //random Id added here becuase it will not take page from the browser cache.. sometimes required for some browser.

window.opener.location.href = parentURLArr1[0] + qStr;

//window.opener.location.href = window.opener.location.href; //this also used to refresh the parent window.
//window.opener.location.reload(); //this also we can add to reload the parent window.
//window.opener.close(); //parent open closing

function closeChild() {
<body onunload=”refreshParent();” >
Child File <br />
<a href=”#” onclick=”closeChild();”> Close Child</a>

This is just demo with alert and other things. so, can check in your local pc.. and you can modified code as per your requirements. 🙂

Run parent.php file to check this exmple.


Nilesh Prajapati.



  1. Nice site.. it is helping me in my works.. thanks nilesh..

  2. Good work

  3. Thanks Nilesh

  4. Thanks a lot. This was a life saver!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: