PDA

View Full Version : javascript reload parent window


lauras2004
Jan 27, 2004, 10:12 AM
Hi all,

I'm trying to submit a form from a popup window and when the user hits the "Submit" button, the parent window should reload.

It works on Windows IE and Netscape, and Netscape 7 on the mac, but so far I can't get it working on IE5 for the mac.

I'm using <onClick="parent.window.location.reload()">

Thanks!!

LauraS.

arson
Jan 27, 2004, 11:09 AM
Here is an example... this is your form page, I called it popupform.html
<html>

<head>
<script language="JavaScript"><!--
var windowReference;

function openPopup() {
windowReference = window.open('popupform2.html','windowName');
if (!windowReference.opener)
windowReference.opener = self;
}
//--></script>
</head>

<body>
<a href="javascript:openPopup();">Open Pop-up</a>
<form method="GET" name="formName" onSubmit="alert('submit triggered')">
<input type="hidden" name="example" value="foo">
</form>

</body>

</html>

and this is the pop-up window, I called it popupform2.html

<form>
<input type="button" value="Submit" onClick="opener.document.formName.submit()">
</form>

lauras2004
Jan 27, 2004, 12:44 PM
Ok, thanks for that ... I *kind of* see what's going on, although when I click the Submit button from the popup window, the first window does appear to reload but the "alert" isn't happening. That's not essential ... so I guess it's working so far.

Problem is, I'm not using javascript to open up the popup window. I'm just opening it using target="_blank". And somehow I think that's going to matter ...

Any (more) wisdom you can apply to this is much appreciated!

LauraS.

jeremy.king
Jan 29, 2004, 01:01 PM
When cross-scripting windows, its always a good idea to "pop-up" new windows with a window.open() command. This will ensure that the javascript window hierachy is observed. This would allow you to access the "parent" window from the popup with the opener notation.

So a good idea may to be substitute
<a href="somepage.htm" target="_blank">

with
<a href="javascript:void(0);" onclick="javascript:window.open('somepage.htm', id); return false;">

Don't worry about the href as it will never be executed due to the return false in the onclick event. Also, that id is any string identifier you would like to use for your popup. That space in the word javascript should be removed (stupid code formatters :rolleyes: )

Finally, you may encounter security issues if you try to control an opener window if the popup and the opener do not belong to the same domain name.

jeremy.king
Jan 29, 2004, 01:04 PM
And to reload the parent window from the popup you can include this code in the popup in the appropriate location.

opener.location.reload();

Good luck.

lauras2004
Jan 29, 2004, 01:06 PM
Thanks for the info. Unfortunately because of standards set by my company, we're not using javascript to open popup windows at all -- only target="_blank".

It's worth noting that even though I didn't use javascript to open the new window, window.opener.location.reload() DOES work on the Windows platform. It even works with Netscape on the mac. It only breaks down when using mac / IE ...

Anyway, thanks again.

Laura S.