Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect when popup window is closed

Status
Not open for further replies.

rob51383

Programmer
Joined
Jun 23, 2004
Messages
134
Location
US
I have one .cgi page open, the user clicks a link/button which opens another window, modifies some form filelds, then clicks the submit button. When the submit button is clicked some data is inserted into a database and the popup window is closed. I would like the first window to refresh so the new data inputed into the popup window is reflected. How do i create the connection between the windows so the first window knows when the second window is closed.

Thanks in advanced.
 
Ok, I understand the concept but not the application.

I understood that on the second window I would code the submit button like so:

<input type="submit" onclick="self.opener.refresh();">

But how does it know what window to refresh?
Do I declare the first window as "self.opener" in the body, the link, or with some actual javascript.

I would really appreciate a coded example.



 
It's automatic. "self" is the current window, "opener" is the window that opened it.

Off the top of my head, the main window:
Code:
<html>
<head>
<title>Main Window</title>
</head>
<body>
<a href="JavaScript:window.open('','');">New Window</a>
</body>
</html>
[code]

The popup:
[code]
<html>
<head>
<title>Child Window</title>
</head>
<body>
<form onsubmit="self.opener.refresh(); self.close();">
<input type="submit" />
</form>
</body>
</html>



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Doesnt work from my end. Internet explorer gives the blocked activX/popup alert too.
 
That's because you're running it locally on Windows XP with Service Pack 2. Just tell it okay to run the script.

And WHAT does it do or not do? Just saying "It doesn't work" is pretty much useless for anyone to try to help you.

Lee
 
Sorry,
The main window's HTML is replaced with "[object]" and the second window opens up.
When you click the submit button the second window refreshes but does not close.
The main window remains in the same state.

Is there another way to do it thats more service pack 2 friendly?
 
Whatever page you submit the information should refresh the parent page and close the window. Otherwise it will be closed before the data is written to the database.

The following will take care of the page having the word [ object ] at the top:

Code:
<script language="javascript">
function newWindow(url)
{
window.open(url,'')
}
</script>
</head>

<body>
<a href="" onclick="newWindow('untitled2.html');return false">New Window</a>

Lee
 
The popup window is still refreshing itself and nothing happens to the first window, "[object]" no longer appears. I want the popup window to submit the form and then close.

I want the main window to re-fresh.
 
Try this for the main page:

Code:
<html>
<head>
</head>

<script language="javascript">
var checkvar = false;
var thispage=location.href;

function newWindow(url)
{
window.open(url,'');
checkvar=false;
}

function refresh()
{
if (checkvar)
  {
   location.href=thispage + '?';
  }
}

setInterval('refresh()', 500);
</script>
</head>

<body>
<a href="" onclick="newWindow('untitled2.html');return false">New Window</a>

</body>

</html>

and put this on the child page:

Code:
<body onload="window.opener.checkvar=true; window.close();">

I tested it in IE 6 and Netscape 4.79, so it should work in most, if not all, browsers.

Lee
 
I tested this too, and surprise, self.close() did NOT work. I'm not sure why, and I AM sure that I've done this on other sites. I need to dig through some old projects. I know that I didn't have to pull any tricks with timers.

Could it be newer browsers have for some reason disabled self.close()?

I had other things to do, but now I know I'll have to track this down.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
You said you tried that, too, but self.close() did not work. I didn't use self.close() anywhere, so you tried something else.

Lee
 
Hello all,

If I can make a working example combining valid points from what proposed, here it is.
Code:
<!-- parent.htm -->
<html>
<head>
<script language="javascript">
var showevidence=new Date();
function newWindow(url) {
    window.open(url,'')
}
</script>
</head>

<body>
<a href="" onclick="newWindow('child.htm');return false">PopupWindow</a>
<br />
<p id="showgettime"></p>
<script language="javascript">
    document.getElementById("showgettime").innerText="new time : " + showevidence.getTime();
</script>
<button onclick="document.location.reload();">reload page</button>
</body>
</html>
And the popup be this.
Code:
<!-- child.htm -->
<html>
<head>
<title>PopupWindow</title>
</head>
<body>
<form>
<input type="button" value="refresh parent" onclick="self.opener.parent.document.location.reload(); self.close();" />
</form>
</body>
</html>
I put a .getTime of Date object to give evidence of refreshing. This construction works.

regards - tsuji
 
Further notes:

It is not that self.close() does not work, it is the upper script line which does not work.

- tsuji
 
Tsuji, that works perfect, thanks.
Now If I could bother you guys once more (on this issue). That blocked content that goes with service pack 2, is there any way around that. Maybe even if I have to do this with another programing language. That alert can really turn off alot of people...
 
I believe that the window might not be blocked if it is opened directly from the page, as in
Code:
// Do this
<a href="newpage.html" target="_blank" onclick="window.open(this.href, 'newwin'); return false;">New window</a>

// Not
<script type="text/javascript>
function newWin(url)
{
  window.open(url, "newwin");
}
</script>
<a href="newpage.html" onclick="newWin(this.href); return false;">New window</a>
Not tested though...

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top