referencing objects on the opener from a pop up

I have a page that opens a popUp.  I am retrieving the values of a (previously stored) array on the opener in the popUp with success.   I would like to restore the values of this previously saved array (from the opener which now resides on the popUp) into fields on the opener after a refresh (window.opener.history.go(0);) when I close the popUp (kind of a poor man's state management.) The problem I'm encountering is that I can't seem to reference the fields on the opener.  I've used eval and referencing the name of the field in the forms collection on the opener with no success.  Please see code below with commented out failed attemps.


NOTE: the elements (fields) on the opener are rendered from .Net, their names contain ":"'s and I have no control over their naming.

Any help ?

function restoreOpenerState()
{
for (var i = 0; i < objStateArray.length; i++)
     {
    var ctlTextName = objStateArray[i].ctlTextName;
    var ctlTextValue = objStateArray[i].ctlTextValue;
                              
    var ctlTypeName = objStateArray[i].ctlTypeName;
    var ctlTypeValue = objStateArray[i].ctlTypeValue;
                              
    var ctlArrayName = objStateArray[i].ctlArrayName;
    var ctlArrayValue = objStateArray[i].ctlArrayValue;
                              
    //opener.document.forms[0][ctlTextName].value=ctlTextValue;
    //opener.document.forms[0][ctlTypeName].value=ctlTypeValue;
    //opener.document.forms[0][ctlArrayName].value=ctlArrayValue;
    //returnVal(ctlTextName,ctlTextValue);
    x=opener.document.getElementById(ctlTextName);
    //eval("opener.document.Form1." + ctlTextName).value = ctlTextValue;
   
    alert(x);
    }
}
mbwatkinsAsked:
Who is Participating?
 
DanRollinsCommented:
It also works if I set an element of an array

(in main)
var anData= new Array();

function ViewVar() {
      alert( anData[3] );
}

(In popup)
function SetVar() {
      window.opener.anData[3]= 888;
}
0
 
DanRollinsCommented:
It seems to work fine for simple variables:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= file: main.html

<HTML><BODY>
Main Window<br>
<input type=button value="OpenPopup" onclick="OpenPopup();">
<input type=button value="ViewVar" onclick="ViewVar();">
<script>
var nTestValue=0;

function OpenPopup() {
      window.open("popup.html",null,
      "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
}
function ViewVar() {
      alert( nTestValue );
}
</script>
</BODY></HTML>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= file: popup.html
<HTML><BODY>
<script>
function SetVar() {
      window.opener.nTestValue=1234;
}
</script>
The popup window<br>
<input type=button value="Set Value" onClick="SetVar()">
</BODY></HTML>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
After a refresh of the main window, the variable is set back to 0, but I can atill change the variable's value from the popup window.

In the test, I click a button in the popup to tell it to send data to the opener.  What mechanism are you using to tell the popup to restore your 'state' values to the main window?

-- Dan
0
 
mbwatkinsAuthor Commented:
I'm sorry I did not make it clear that the array on the popup contains the name and value of each control on the opener.  What i need is the syntax to 1. locate the element on the opener, and; 2. to set its value.  Based on Dan's reply I will try just repopulating the array on the opener and then have the opener set the element's values.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DanRollinsCommented:
What mechanism are you using to tell the popup to restore your 'state' values to the main window?
 -- or --
Are you having the Main window query the popup?
The obvious problem with the latter technique is that after Main is refreshed, the reference to the popup is lost and if you re-open the popup, all of its variables will be reset.
0
 
DanRollinsCommented:
BTW, if you want to maintain state across a refresh, why not use the mechanism that is designed to do that?

function SetCookie(sName, sValue) {
        document.cookie = sName + "=" + escape(sValue) + "; expires=Mon, 31 Dec 1999 23:59:59 UTC;";
}

function GetCookie( sName ) {
        var aCookie = document.cookie.split("; ");
        for (var i=0; i < aCookie.length; i++)  {
                var aCrumb = aCookie[i].split("=");
                if ( sName == aCrumb[0] ) {
                        return unescape(aCrumb[1]);
                }
        }
        return null; // a cookie with the requested name does not exist
}
0
 
mbwatkinsAuthor Commented:
Because of company requirement to not use cookies, I used you suggestion to repopulatre the opener's array (rather than the controls on the opener).  Thanks!
0
 
DanRollinsCommented:
Glad I could help!  I'll try harder next time so that I can earn an A.
-- Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.