Categories:

Sending objects from one page to another

Credits: This tutorial is written (except for current page) and contributed by Alex Vincent. Edited by JavaScript Kit. Alex is a long time visitor of JavaScript Kit, and participant of the JK Help Forum.

    One of the most common requests we get in the JavaScript Kit forum is "How do I send a variable to the next page?"  In this tutorial, we're going to give you several examples of doing precisely this. Specifically, we're going to cover:

Location.search

Have you ever seen a web address like this: "http://mysite.com/mydirectory/mycgi.cgi?adfjwa4n=yes" ?  This, when loaded into a window, fills the window.location object with several components.  This object has many properties -- and one of the properties most underused in JavaScripting is the window.location.search object.

This object, simply put, is a string which holds everything following the question mark in a page (including that question mark.)  For instance, with the above URL, the window.location.search property would have the value "?adfjwa4n=yes".  How can you use this information?

First, you need to send the information.  Here's some sample code:

<HTML>
<HEAD>
<SCRIPT Language="JavaScript">
<!--
function nextpage() {
// I want to send on the MyValue element of the form below.
window.location.href = "send2.htm?MyValue.value='"+document.forms.myForm.MyValue.value+
"'&MyTest.value='"+document.forms.myForm.MyTest.value+"'"

}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myForm">
<INPUT TYPE="TEXT" NAME="MyValue" SIZE="5" VALUE="AEIOU">
<INPUT TYPE="BUTTON" onClick="nextpage()" VALUE="Go to the next page">
</FORM>
</BODY>
</HTML>

Try it out here. You can change the text to whatever you want, and it will show up as you typed it in the next page.

Copyright 1997-2014 JavaScript Kit. NO PART may be reproduced without author's permission.