Solved

referencing objects on the opener from a pop up

Posted on 2004-04-10
7
834 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now