Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q247 Do the values of variables in *.js source files last through a frame/document reload?

You are here: irt.org | FAQ | JavaScript | Source | Q247 [ previous next ]

If you've included a JavaScript *.js source file in one of your frames or documents, and then set the values of variables therein, when you reload the frame you lose that instance of the source file plus the value of any of the variables. The JavaScript *.js source file does however remain available in the browsers cache. If you require a way of maintaining state through the reload of a document then I would recommend using frames.

Store the variable values in the parent frame. They then remain available up until the point when the parent frame is reloaded. For example:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
var myvariable = '';
//--></SCRIPT>
</HEAD>

<FRAMESET ROWS="50%,*">
<FRAME SRC="apage.html" NAME="upper">
<FRAME SRC="bpage.html" NAME="lower">
</FRAMESET>
</HTML>

Then in either upper or lower you can set or create variables in the parent frame:

parent.myvariable = 'xyz';  // amends the value of myvariable

Or:

parent.anothervariable = 123;  // creates and sets a new variable

If you reload the frame then you can retrieve the values as follows:

var myvariable = parent.myvariable;
var anothervariable = parent.anothervariable;

©2018 Martin Webb