Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

referencing objects on the opener from a pop up

Posted on 2004-04-10
7
Medium Priority
?
841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
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.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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…

722 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