Using the frameset document

One of the greatest challenges to writing JavaScript is that when the user leaves a page, all of the functions and variables are lost. One way to overcome this on a site is to place variables and functions in the frameset document. For example, navigational functions such as previous and next buttons for sequential surfing can be implemented by tracking the current page in the parent document. The buttons themselves can be placed in a frame of their own, and call a function on the parent document to move the document. The parent document could hold an array of pages to scroll through, an index variable marking the current page, and a function that moves to the next or previous document. This move function could be implemented by using the following code in the parent frame:

<script language="JavaScript">
<!-- Hide script

var currentPage=0;

var pages = new Array();
pages[0] = "welcome.htm";
pages[1] = "mission.htm";
pages[2] = "goals.htm";
pages[3] = "thanks.htm";

function move(direction)
{

currentPage += direction;

if(currentPage == pages.length)
	currentPage--;
else if(currentPage == -1)
	currentPage = 0;

page.document.location.href = pages[currentPage};
}
// End Script -->
</script>

This code makes use of arrays and the new window and location properties. The variable 'currentPage' is used as an index to the pages array. The pages array holds the string values of each document's location. Finally, the move function is responsible for updating the page. The parameter 'direction' accepts a numeric argument of either 1 or -1. The code then checks to see if the currentPage variable is equal to the length of the pages array. Because array indexes begin with 0, the length property is always one greater than the last element. If the currentPage variable is out of bounds, it is set back to the first or last element. Finally, the code uses the location object of the page frame's document to set its 'href' value to the string in the pages array indexed by the currentPage variable. This has the effect of loading the next or previous page in the browser window.