1. Home
  2. Computing & Technology
  3. JavaScript

Passing Parameters Via Query Strings

2. The Read Function

Join the Discussion

Questions? Comments?

More of this Feature

Write Querystrings Read Querystrings

We can easily set up a Javascript function that will retrieve the query string and load an array with the values passed in the query string. Here it is. This function goes into the head section of our page.

var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}

The only other thing that we need to do is to initialize entries in the array for the fields that we want the page to process and to actually call the above function to read the querystring.

1 | 2 | 3
Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.