Solved

referencing objects on the opener from a pop up

Posted on 2004-04-10
7
836 Views
Last Modified: 2008-02-01
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
Comment
Question by:mbwatkins
  • 5
  • 2
7 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 10800434
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
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 10800455
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
 

Author Comment

by:mbwatkins
ID: 10801081
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 49

Expert Comment

by:DanRollins
ID: 10802147
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 10802177
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
 

Author Comment

by:mbwatkins
ID: 10807837
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 10808772
Glad I could help!  I'll try harder next time so that I can earn an A.
-- Dan
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question