Forum Moderators: open

Message Too Old, No Replies

window.open problem

         

wizzz

4:10 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



If someone click the link in main window a new window will(second window) open, but how can i refresh the main window automatically after closing the second window.

Fotiman

4:46 pm on Jul 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the second window, before closing you could access the window.opener (which is a reference to the window that opened the second window).

rocknbil

6:14 pm on Jul 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd need to do it on some event in the open window, namely,

<body onUnload="window.opener.document.reload();">

Although if the opener requires variables for it's current state, this won't work (example: you're on a page to edit data, which requires a record ID to get to that page.) So a function that accepts the parameters you need to get to that state would work in combination with document.location:

<body onUnload="refreshMain('1234','edit_record');">


function refreshMain(pid,state) {
if ((! pid) ¦¦ (! state)) {
alert('Record ID and script state required to close and refresh.');
return;
}
if (window.opener && !window.opener.closed) {
window.opener.document.location='yourscript.cgi?yourfunction='+state+'&record_id='+pid;
window.close();
}
}

Note that ¦ must be replaced with logical OR pipes.

wizzz

8:55 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



i will put this in opened window?
<body onUnload="window.opener.document.reload();">

this in main window?
<body onUnload="refreshMain('1234','edit_record');">

and function in main window?

Is this working in all browsers?

rocknbil

8:57 pm on Jul 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No those are "either/or," and would go in the pop-up. To my knowledge, no cross-browser issues. If the main page (the opener) is a static page or script page that doesn't need variables to recreate on refresh, use reload(). If it does, use the function as described.