Link to home
Start Free TrialLog in
Avatar of troyd1
troyd1

asked on

javascript updating grand parent window

I have a situation where I want to in some cases update a field in the parent window and in some cases update a field in the grand parent window. How would I do this?

I have done opener.document.form1.FIELD.value='newvalue'.

How can I do first.opener.document.form1.FIELD.value='newvalue'

Thanks
Avatar of ellandrd
ellandrd
Flag of Ireland image

explain what you mean by grand parent window??

cheers
sean
Avatar of justinbillig
justinbillig

like you have your main page, that opens a window, that page opens a window

it would be


window.opener.opener.document.form1.FIELD.value - 'newvalue'
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this:

this code opens a window and then opens an popup window (you can change it to open another window for your example..)

<html>
<head>
<script language="javascript">
<!--
var ls_value = 0;
function call() {
     newWin =window.open("childwnd.html", 'TheNewWin', 'height=500, width=700, top=180, left=180,fullscreen=no, scrollbars=yes, resizable=yes, menubar=yes, toolbar=yes, location=yes, directories=yes, status=yes');
}

function getValue() {
     document.write("Value is : " + ls_value);
}
//-->
</script>
</head>
<body onload="call();">
<a href="#" onClick="getValue();">Value from pop window</a>
</body>
</html>


<###### HTML code for childwnd.html ######>
<html>
<head>
<script language="javascript">
<!--
function call_return() {
     //var ls_text = document.frm_child.text1.value
     var ls_text = document.frm_child.t1.value;


     self.opener.ls_value = ls_text
     self.close();
}
//-->
</script>
</head>
<body>
<form name ="frm_child">
<input type="text" name="t1" size="20">
<input type="button" value="Submit" name="btn1" onClick ="call_return();">
</body>
</form>
</html>

sean
Avatar of troyd1

ASKER

I think what I am proposing is not possible, but I like the suggestions. I will try to explain further:
main window is unnamed, but we will call it A. B has code that updates a form field in A. A will always be the root window.
B can be opened by A or B can be opened by C which was opened by A.

ex.
A > B
A > C > B

I want to generically tell B to update A without giving A a name. Somehow saying root.opener.document.form1.ABC.value = whatever.

I think zvonko's solution is the best, but not exactly what I was asking.

Thanks, Troy
The window A can assign a name to itself and pass it to children and they pass them to grandchildren.
Avatar of troyd1

ASKER

I guess that I am asking if you can reference the root window without giving it a name.
The root window can be referenced only in a frameset as _top or in DOM as top
Otherwise you cannot list all top windows and select one you like to use.
And why do you like to avoid giving the window a name?
Its such easy like this:
<script>
window.name = "myTopWindow";
</script>

Thanks.