Passing parameters between pages


It's really easy to pass parameters between web pages - you just put the parameter string, preceded by ?, after the name of the page in your hyperlink or JavaScript method call. Here are some examples:

<A HREF="newpage.htm?Parameter data string">

location.href="newpage.htm?Parameter data string"

window.open("newpage.htm?Parameter data string")

In each case, the target page ('newpage.htm') would expose a location.search property with a value of '?Parameter data string'. Scripts in the page can then do whatever they want with that data (see Web Workshop, PC Plus issue 156 for more information).

Sometimes you might want to pass more than one item of information to another page. To do this, you need to assemble them into a single parameter string in the calling page, then break the string back up in the target page. Here's an example of assembling a multi-item string:

imgname="mypic.jpg"
caption = "A Nice Picture"
targetname = "newpage.htm?" + imgname + "&" + caption
location.href=targetname

The value of targetname would be "newpage.htm?mypic.jpg&A Nice Picture", and newpage.htm's location.seach property would contain "?mypic.jpg&A Nice Picture" - a filename and some caption text, separated by a & character.

Here's a script, which you can place in the <HEAD> section of your target page (the one that receives the parameter string). It will break the string up into however many items it contains, using & as the separator character. Ihe items are placed in an array called page_params[].

<script>
/* Paul Stephens' location.search string parser script

Put this script at the start of the <HEAD> section of your page, so that the
parameters are available to any other scripts in the page. It contains inline code, so
doesn't need to be called from anywhere.

This script expects a multi-item parameter string in the format:

?value1&value2&value3

where '&' is a separator character (if you want to use a different separator character,
change the value of 'sepchar' below)

It transfers the individual values into an array, page_params[].

The location.search string can contain any number of values.

*/

// Change the value of 'sepchar' below if you want to use a different separator character
var sepchar = "&" 

page_params = new Array()
var data_count = 0

ds = location.search
ds+= sepchar 
var sl = ds.length
var startidx = 1
var endidx = 0
var cstring

if (sl > 1) {

do 
{
endidx = ds.indexOf(sepchar, startidx)
if (endidx !=-1) {
cstring = ds.substring(startidx, endidx) 
startidx = endidx + 1

// unescape method removes code strings such as %20 for space.
page_params[data_count] = unescape(cstring)
data_count++}
}
while (endidx !=-1 & endidx != (ds.length -1));

}

// ****** end of Paul Stephens' parameter-handling script
</script>

If the page was called like this:

<A href="newpage.htm?mypic.jpg&A Nice Picture">

then after this script had executed, page_params[0] would contain "mypic.jpg", and page_params[1] would contain  "A Nice Picture".


Back to menu