Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

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);
    }
}
0
mbwatkins
Asked:
mbwatkins
  • 5
  • 2
1 Solution
 
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
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now