Querystring parameters


Notes on Query Strings

A "Query String" is part of a web page URL, containing data to be passed to the page. It follows the web address, and is separated from it by a single question mark. It can contain any number of parameters, which are separated by ampersands. A parameter name and its optional parameter value are separated by an equal sign. There are no quoting delimiters, so the parameter value cannot contain equal signs or ampersands.

Querystrings may be part of the href in a hyperlink, or may be generated by a web-based form. Typically, query strings are used by server-side applications, such as CGI or ASP programs, but they can also be interpreted and used by JavaScript programs that run in the web browser.

This page uses client-side JavaScript to interpret the query string. It utilizes an external JavaScript file called querystring.js to load the query string into an associative array called queryString. (An "associative array" is just an array that uses string subscripts.)

Annotated Examples:

These hyperlinks all link back to this page, with different querystring parameters to illustrate how to handle special characters.

  1. querystring.htm?footwear='boots&shoes'
    Note that quotes are just characters within a querystring value; they serve no delimiting role. But characters such as & and = have special roles the query string, they must be encoded to be used in the values themselves.  So the query string in this example is not valid; 'boots&shoes' represents the value 'boots followed by the invalid parameter name shoes'
  2. URL-encoding the ampersand
    Character encoding in URL's is called "URL-encoding" (or, more formally, but less commonly "URI-encoding").  Fortunately, URL-encoding is easy.  Form field values are automatically URL-encoded when the form is submitted, and you can use the JavaScript escape and/or URIencode (that's a capital I, not a lowercase L) functions to URL-encode any string with   Or you can manually URL-encode characters by using the percent-sign (%) plus the Hex ASCII code for the character. See a table of ASCII values below.  This example shows the use of %26 to represent an ampersand.
  3. Some URL-encoded characters
    This hyperlink includes several URL-encoded characters.  After you click on the hyperlink, you'll see how the characters are URL-encoded in the querystring, and how they are URL-decoded by the JavaScript querystring function. 
  4. querystring.htm?footwear=boots%26shoes%20%40%%2025%25%20off
    You figure this one out ☺
  5. Finally - Spaces in querystring parameters
    Technically, there should be no spaces in URLs, either in the address or in the query string. They do work most of the time (and you'll probably see the spaces work in this example, but they are somewhat dangerous. The most proper encoding for spaces is %20 (Hex ASCII value 20, or decimal 32), but the plus sign is also supposed to work, and is generally used when forms encode querystring parameters. Unfortunately, the JavaScript built-in functions escape and URIencode (and their corresponding decoding functions unescape and URIdecode) assume spaces are encoded as %20, and they do not convert plus signs. The Javascript code in querystring.js uses the string replace method plus the unescape function to make JavaScript handle query strings properly.

    Enter some special characters to see how they are translated:

    Note that since special characters are automatically URL-encoded by forms, you can't enter an URL-encoded string in a form field and expect it to be decoded by the action script.  That is, since percent signs are URL-encoded, %25 (the URL-encoding of a percent sign), would be converted to %2525, and thus would be decoded as %25 rather than %. So here's an URL-encoding widget:
    URL-encoded string:
    decoded string:
    (As it turns out, HTML forms and JavaScript functions do URL-encoding slightly differently, so don't be surpised if some characters get encoded by one method but not the other. Fortunately, most characters that are treated differently don't have common special uses in URL's, so the difference shouldn't matter.)

Using querystring.js to process an HTML form

The javascript file used by this page is a general-purpose parameter-reading function.  Any HTML page that utilizes it can read any incoming querystring parameters, such as those supplied by a form. To use it

  1. Make sure that a copy of querystring.js is available to your form action page,
  2. Make sure that your form uses method="get", and
  3. Insert this script tag in the HTML header of the action page. ,

The JavaScript file will assure that, In your action page, the JavaScript array "queryString" will be loaded with all of the form data.  You can then display the data, or use it in any way you wish. (Note that the queryString JavaScript file inserts code into "main line" of the page, rather than in a JavaScript function.  The code is thus executed as the page is loaded, without any function call in the HTML file.)

To see this process in action, fill out this short form. You'll invoke this very page, so you can see your querystring parameters at the top of the page.  Then scroll down to the pink box below to see how the values can be displayed.

Name:
Email address:

The JavaScript code

Querystring.js contains only a few lines of code.  Here it is:

/* querystring.js - Contains functions to extract querystring parameters from URL
The parameters are loaded into the associative array queryString[].

//In open JavaScript (not inside a function), define the array
var queryString = new Array();
// and then pull the querystyring parameters from the URL.
// The search property of the window location returns the query string.
// The method substring(1) removes the first character (the question mark).
// The split function then copies the parameters into an array called "parms"
var parameters = window.location.search.substring(1).split('&');
// For each element in the array, find the equal sign that separates the parameter
// name from the parameter value.  If there is one, divide the expression into
// the parameter name
for (var i=0; i<parameters.length; i++) {
    var pos = parameters[i].indexOf('=');
    // If there is an equal sign, separate the parameter into the name and value,
    // and store it into the queryString array.
    if (pos > 0) {
        var paramname = parameters[i].substring(0,pos);
        var paramval = parameters[i].substring(pos+1);
        queryString[paramname] = unescape(paramval.replace(/\+/g,' '));
    } else {
        //special value when there is a querystring parameter with no value
        queryString[parameters[i]]="" 
    }
}

Then this web page displays the querystring parameters with this JavaScript loop, which displays all of the elements in the associative array:

Finally, the JavaScript code that displays the results of the form submission is: JavaScript code much like this would be used by any HTML form processing processing that uses querystring.js.

ASCII Values for URL-encoding

This table shows Hexadecimal and Decimal values for some standard characters.  It is important to remember that URL-encoding uses Hex values and a percent sign, while HTML entities use decimal values and a pound sign to represent the same characters.  Confusingly, HTML colors use a pound sign and Hex values!

 HexDec
space2032
!2133
" 2234
#2335
$2436
%2537
&2638
'2739
(2840
)2941
 HexDec
*2A42
+2B43
,2C44
-2D45
.2E46
/2F47
03048
13149
...  
93957
 HexDec
:3A35
;3B36
<3C37
=3D38
>3E39
?3F40
@4064
A 4165
...  
Z5A90
 HexDec
[5B91
\5C92
]5D93
^5E94
_5F95
'6096
a6197
b6298
...  
z7A122
 HexDec
{7B123
|7C124
}7D125
~7E126
del7F127

5/5/2007 Robin Richmond, for the HTML/Javascript Class (ITWM1010) at Cuyahoga Community College.