Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

variable carryover

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder. Suppose I have two html files written in HTML and JavaScript (which I wrote), called page1.html and page2.html. All the variables belonging to page1.html will be referred to, as long as page1 is being displayed, as document.variable1, document.variable2, etc., right? The "document." part can be dropped, and the variables can also be referred to as variable1, variable2, etc. right?
Now, suppose I have page1 loaded, and the variables are all set to some value determined by the user input. Then the user clicks on a link and page2 is loaded. But suppose I want the browser to remember the values of the variables in page1. Does the browser forget the page1 variables? If not, how should I refer to the page1 variables? How is this usually done? Do you go to page2 and then refer to page1 variables? Or do you set page2 variables based on the page1 values when loading page2 and forget page1 variables?
 
Sheriff
Posts: 67528
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each page is an island onto itself. You can't refer to variables in pages that aren't loaded.

If you need the data values from the first page in the second, you'll need to: pass them as request parameters, or save them in a cookie, or use a server-side session to retain them.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you created the window with window.open and the parent is still open and has not refreshed. You can use

var foo = window.opener.variableName;

Eric
 
Bear Bibeault
Sheriff
Posts: 67528
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, should have mentioned that. The same is true of pages in frames and iframes (though with different referencing). The point is that only loaded pages are active. You cannot reference info from pages that are no longer loaded; as in serial, rather than parallel pages.
[ March 27, 2007: Message edited by: Bear Bibeault ]
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About
var foo = window.opener.variableName;

Is this a statement which appears in the JavaScript code for the newly opened window? And the variableName refers to a variable in the window which did the opening?
Can you call a function the same way, like
INPUT TYPE="button" onclik="window.opener.myFunction()"
?
 
author
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way of doing this that I used to use in the olden days was to put a single frame inside the main page, and store the JS code in the frameset, so that the full visible page (i.e. the frameset) could refresh without the variables being lost.

From the frames, you'd refer to variables in the frameset as top.myVariable.

However, if you're writing an app that needs to maintain a lot of state and/or code on the client, you really ought to have a look at Ajax, which solves this problem in a much better way IMO, by allowing you to fetch data and/or content from the server without refreshing the page at all. Then all your JS variables can just be referred to straightforwardly.

Cheers,

Dave
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Tysen:
About
var foo = window.opener.variableName;

Is this a statement which appears in the JavaScript code for the newly opened window? And the variableName refers to a variable in the window which did the opening?
Can you call a function the same way, like
INPUT TYPE="button" onclik="window.opener.myFunction()"
?



Did you try it out?

Eric
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic