Using Frames to Pass Values between Pages

JavaScript variables are local to the page which created them. Thus when you link from one page to another, the JavaScript variables on the previous page are not accessible on the new page. Writing a cookie on one page and then reading the cookie on another page provides a way around this limitation.

Using frames provides another way around this difficulty, by writing the values to the other frame, and then the new page can access the values in the other frame as long as the page in the other frame remains the same.

I've constructed an example below to demonstrate this.

passingvalues.html

<html>
<head><title>Using Frames to Pass Values between Pages</title></head>
<frameset rows="80%,*" frameborder=no frameborder=0>
<frame src="contentframe.html" name="content">
<frame src="valueframe.html" name="values">
</frameset>
<html>

contentframe.html

<html>
<head>
</head>
<body>
<script>
i=prompt("Enter value for i", "");
j=prompt("Enter value for j", "");
parent.values.i=i;
</script>
We set two values, i and j, in this page. We will now link to another 
<a href="contentframe1.html">page</a> and show that we can't access the
variable j on the former page from this page. However, the top frame writes to
a variable in the bottom page the value of i in the top frame. When the page on top
changes, the new page can read the value in the bottom frame, which 
effectively gives the new page read-access to the value in the previous page.<p>

This shows how you can use frames, even a hidden frame, to pass values between 
pages, just as you can use cookies to pass values between pages.
</body>
</html>

valueframe.html

<html>
<head>
</head>
<body>
This page displays a variable i which has been created by a page in the top frame. 
When the page in the top frame changes, the new page can access the value in 
the bottom frame, and thus effectively has read-access to the value from the 
previous page in the top frame.
<script>
document.write("The value of i in the other frame is " + i);
</script>
</body>
</html>

contentframe1.html

<html>
<head>
</head>
<body>
<script>
document.write("The value of i is " + parent.values.i);
document.write("The value of j is " + j);
</script>
We set two values, i and j, on the page which referred to this page. The
bottom frame set a value equal to the value of i on the previous page; the new
top page can access this value in the bottom frame, as shown above. An attempt 
to reference j produces an error. 
</body>
</html>