O'Reilly logo

JavaScript & DHTML Cookbook by Danny Goodman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Passing Data Between Pages Via URLs

NN 2, IE 4(Mac)/5(Win)

Problem

You want to move user-influenced data from one of your pages to another without server intervention, cookies, or frames.

Solution

Pass the data as a search string appended to the URL of the next page and include a script in the next page to read the search string to retrieve the data. As a simple case, the following code passes a single value retrieved from a text input field as a search string:

<script language="JavaScript" type="text/javascript">
function goNext(url) {
    var data = document.forms[0].userName.value;
    location.href = url + "?" + escape(data);
}
</script>
...
<a href="page3.html" onclick="goNext('page3.html'); return false">...</a>

In the second document, an onload event handler retrieves the search string data and assigns the value to a text input field with the same name located on the second page:

<script language="JavaScript" type="text/javascript" >
function readData( ) {
    var srchString = unescape(location.search.substring(1, location.search.length));
    if (srchString.length > 0) {
        document.forms[0].userName.value = srchString;
    }
}
</script>
...
<body onload="readData( )">

You can embed both event handlers and functions into all related pages to keep the data moving through all page visits.

Discussion

The magic behind the URL scheme is all versions of Netscape Navigator, IE 4 or later for Macintosh, and IE 5 or later for Windows retain the search string that comes back from a server, even if the server ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required