Request.QueryString() for Javascript
Advanced Manual
Current script version: v1.41 (2007-06-28 18:10 UTC)
- Table of Contents for this page
- Basic information - A Beginners Guide. First time users of the script start here!
- Download [5.29KB]
Contents
Available Commands
The following is a list of the available commands that you can use to get at the URL data when
using this script. In the subsequent examples the parameter “keyName” is a
String corresponding to the variable name of the data value in the URL. For example if the URL looks like:
“example.html?name=Andrew&age=28” then you could substitute the word
“keyName” in the examples for either “name”
or “age”. Also in the examples the parameter X means a positive non-zero
Integer, i.e. X is one of {“1”,“2”,“3”,“4”,“5”, … infinity}.
In reality “X” tends to be the number “1” in almost all cases.
Command Menu
Select a command from the following list to view information about it. Some commands perform the same action. Variants of some commands are not listed.
Request.QueryString.Count
Request.QueryString.Count()
Request.QueryString.Item
Request.QueryString.Item()
Request.QueryString.Item(X)
Request.QueryString.Item("keyName")
Request.QueryString.Key(X)
Request.QueryString.Key("keyName")
Request.QueryString()
Request.QueryString().Count
Request.QueryString().Count()
Request.QueryString().Item
Request.QueryString().Item()
Request.QueryString().Item(X)
Request.QueryString().Item("keyName")
Request.QueryString().Key(X)
Request.QueryString().Key("keyName")
Request.QueryString(X)
Request.QueryString("keyName")
Request.QueryString("keyName")[X]
Request.QueryString("keyName").Count
Request.QueryString("keyName").Count()
Request.QueryString("keyName").Item
Request.QueryString("keyName").Item()
Request.QueryString("keyName").Item(X)
Request.Version
Commands In Detail
“Request.QueryString()” and Equivalent Commands
Request.QueryString();
Request.QueryString.Item;
Request.QueryString.Item();
Request.QueryString().Item;
Request.QueryString().Item();
- Description
- Returns the raw querystring as it appears in the URL after the ? mark
- Example 1
-
In a page with URL: “
example.html?user=Andrew&userId=1562” then “Request.QueryString” would return “user=Andrew&userId=1562”. - Example 2
-
In a page with URL: “
example.html” then “Request.QueryString” would return “”.
“Request.QueryString("keyName")”
- Description
- Returns all of the values corresponding to the name “
keyName” in the URL. - Example 1
-
In a page with URL: “
example.html?user=Andrew&userId=1562” then “Request.QueryString("user")” would return “Andrew” and “Request.QueryString("userId")” would return “1562”. - Example 2
-
In a page with URL: “
example.html?color=blue&color=red&COLOR=green” then “Request.QueryString("color")” would return “blue, red, green” When more than one key name appears in the URL the result is a comma seperated string of the values. Note also that key names are not case sensitive but values are however. - Example 3
-
In the page with URL: “
example.html?” then “Request.QueryString("colour")” would return “null”.
“Request.QueryString(X)”
- Description
-
Returns all of the values corresponding to the “
X”th key in the querystring. The order of the keys is decided by the browser software and in some cases may not agree with the order in which ASP would specify. This is a known issue with Opera 6.0. Given this possibility you should only use this command if you are enumerating over the whole querystring - see second example. - Example 1
-
In the page with URL: “
example.html?user=Andrew&userId=1562” then “Request.QueryString(1)” should return “Andrew”, “Request.QueryString(2)” should return “1562” and “Request.QueryString(3)” would generate an “alert()” error message advising that an index is out of range if error messages are enabled, otherwise “null” would be returned. Given that the ordering of the keys is dependent upon the web browser the first two answers could appear in a different order. - Example 2 — Looping through all of the querystring variables by enumeration
-
In the page with URL: “
example.html?user=Andrew&userId=1562” then:for (var i in Request.QueryString()) {
if (typeof Request.QueryString(i) != "function") {
document.write(Request.QueryString(i) + "<br>");
}
}would generate: “
Andrew<br>1562<br>”. The second line MUST be included and is a consequence of the javascript language.It is up to the browser software to decide in which order to list indexable items. See the example at the Request.QueryString.Item(X) entry for more information.When enumerating (using “in”) you must check to see if the type of the enumerated result is not a function. This is an unfortunate consequence of the javascript language. See the second example at the Request.QueryString.Count entry for a way to loop through all variables in a querystring in a browser-safe way.
“Request.QueryString().Item("keyName")” and Equivalent Commands
Request.QueryString().Item("keyName")
- Description
- Returns all of the values corresponding to the key “
keyName”. - Example 1
-
In the page with URL: “
example.html?user=Andrew&userId=1562” then “Request.QueryString.Item("userId")” would return “1562” and “Request.QueryString.Item("user")” would return “Andrew”.
“Request.QueryString().Count()” and Equivalent Commands
Request.QueryString.Count()
Request.QueryString().Count;
Request.QueryString().Count()
- Description
- Returns the total number of keys in the querystring.
- Example 1
-
In the page with URL: “
example.html?user=Andrew&userId=1562” then “Request.QueryString.Count;” would return “2”. - Example 2 — Looping through all of the querystring variables
-
In the page with URL:
example.html?user=Andrew&userId=1562&age=28&height=1%2E82m&weight=69kgwe can loop through all of the querystring variables with the following script:for (var i=1; i<Request.QueryString.Count; ++i) {
document.write(Request.QueryString.Item(i) + "<br>");
}The output would be: “
Andrew<br>1562<br>25<br>1.82m<br>69kg<br>”.
“Request.QueryString("keyName").Count()” and Equivalent Commands
Request.QueryString("keyName").Count()
- Description
- Returns the total number of values attached to the key “
keyName”. - Example 1
-
In the page with URL: “
example.html?color=blue&color=red&COLOR=green” then “Request.QueryString("color").Count;” would return “3” — remember that key names are not case sensitive so “COLOR” is the same key as “color”.
“Request.QueryString("keyName").Item(X)”
- Description
-
Returns the single value whose index is “
X” from a collection of values belonging to the key “keyName”. The value of “X” will map to the same data on all web-browsers in this case. - Example 1
-
In the page with URL: “
example.html?color=blue&color=red&COLOR=green” then “Request.QueryString("color").Item(3)” would return “green” — remember that key names are not case sensitive so “COLOR” is the same key as “color”. - Example 2 — Looping through all values in a given key
-
In the page with URL: “
example.html?fish=Haddock&color=blue&color=red&COLOR=green” we can loop through all of the values in key “COLOR” with the following script:for (var i=1; i<Request.QueryString("COLOR").Count; ++i) {
document.write(Request.QueryString("COLOR").Item(i) + "<br>");
}The output would be: “
blue<br>red<br>green<br>”.
“Request.QueryString("keyName")[X]”
- Description
-
Returns the single value whose index is “
X” from a collection of values belonging to the key “keyName”. It is functionally identical to “Request.QueryString("keyName").Item(X);” above. The value of “X” will map to the same data on all web-browsers in this case.This is the only command that differs slightly from ASP. In ASP you would use round brackets instead of square brackets such as “
Request.QueryString("keyName")(X)”. - Example 1
-
In the page with URL: “
example.html?color=blue&color=red&COLOR=green” then “Request.QueryString("color")[3]” would return “green” — remember that key names are not case sensitive so “COLOR” is the same key as “color”.
“Request.QueryString().Item(X)” and Equivalent Commands
Request.QueryString().Item(X)
- Description
- Returns all of the values corresponding to the key with index “
X” in the querystring. - Example 1
-
In the page with URL: “
example.html?user=Andrew&userId=1562&age=28&height=1%2E82m&weight=69kg” then “Request.QueryString.Item(4)” should return “1.82m” — remember though that any one of the data values could be returned since it is the browser software that decides the index order of keys in this type of situation. Also note how this wasn't an issue with the previous two commands since this issue only applies to keys and not values.Opera 6+ is one known browser that does not order key indexes in the same way that ASP does. At the time of writing the tested Netscape, Internet Explorer and Opera 5.12 browsers all produce the same index ordering as ASP does.
For Internet Explorer when “
X” is “5” the returned value would be “69kg”, but in Opera 6.0+ the value returned was “1562” — it's as if they are shuffled randomly. To remind you once again if you are indexing values in a given key then this issue does not apply, for example “Request.QueryString("keyName").Item(1)” will return the same value no-matter what web-browser is being used, the issue only applies when no key name is specified.It is up to the browser software to decide in which order to list indexable items.
“Request.QueryString().Key(X)” and Equivalent Commands
Request.QueryString().Key(X)
- Description
- Returns a String of the name of a key whose index is “
X”. - Example 1
-
In the page with URL: “
example.html?user=Andrew&userId=1562&age=28&height=1%2E82m&weight=69kg” then “Request.QueryString.Key(3)” should return “age” — remember though that any one of the data values could be returned since it is the browser software that decides the index order of keys in this type of situation. Opera 6.0+ shuffles the key index values around, for this example Opera 6.0 returned “height”.It is up to the browser software to decide in which order to list indexable items. See the example at the Request.QueryString.Item(X) entry for more information.
“Request.QueryString().Key("keyName")” and Equivalent Commands
Request.QueryString().Key("keyName")
- Description
-
Returns a String of the name of a key whose keyName is “
keyName”. This may sound very odd at first glance! If the “keyName” is not found then a null string “” is returned. This is useful for checking if a certain key name exists before attempting to retrieve data values for that key name. - Example 1
-
In the page with URL “
example.html?user=Andrew&userId=1562” thenif (Request.QueryString.Key("userId") != "") {
document.write(Request.QueryString("userId"));
}
else {
document.write("The userId data was not passed to this page.");
}
“Request.QueryString("keyName").Item()” and Equivalent Commands
document.write(Request.QueryString("user")[i] + "<br>");
}
- Description
-
Returns all of the values corresponding to the key with name “
keyName”. It is identical in functionality to “Request.QueryString("keyName")”. - Example 1
-
in page with URL “
example.html?user=Andrew&userId=1562” then “Request.QueryString("user").Item()” would return “Andrew” and “Request.QueryString("userId").Item()” would return “1562”. - Example 2
-
In the page with URL “
example.html?color=blue&color=red&COLOR=green” then “Request.QueryString("color").Item()” would return “blue, red, green” When more than one key name appears in the URL the result is a comma seperated string of the values. Note also that key names are not case sensitive, values are however. - Example 3
-
In the page with URL
example.html?then “Request.QueryString("colour").Item()” would return “null”
“Request.Version”
- Description
- Returns a number indicating the current version of the script
- Example 1
-
document.write(Request.Version);
Produces “
0.6” for version 0.6 Beta.
Issues
Key indexing
Keys may be referenced using an index rather than by their name. However, in certain browsers - namely Opera 6.0 - the ordering of the key indices is somewhat random (an example follows). This should not present too many problems since you only really need to use key indices when you are looping over all keys. Internet Explorer, Netscape Navigator and even Opera 5.12 seem to assign index numbers to their keys in the same wasy as ASP does, the reason for Opera 6.0+'s randomness is not currently known.
Example:
Given a page with a URL as:
“example.html?user=Andrew&userId=1562&age=28&height=1%2E82m&weight=69kg”
ASP would equate “1” to “user”, “2” to “userId”,
“3” to “age”, “4” to “height” and “5”
to “weight”. Internet Explorer, Netscape Navigator and Opera 5.12 follow suit such that
Produces “height”. Opera 6.0 assigns the key indices as follows:
“1” to “user”, “2” to “age”,
“3” to “height”, “4” to “weight” and “5”
to “userId”.
When referencing values in a given key this problem does not appear due to the way key values are
stored. So given a URL as “example.html?color=blue&color=red&COLOR=green” then
will always produce “green” regardless of whether or not you are using Opera 6.0
or Netscape Navigator etc.
Enumeration
The “in” statement allows you to loop through the querystring or a given key with a
minimal amount of script:
document.write(Request.QueryString("user")[i] + "<br>");
}
This is shorter than the similarly functional:
document.write(Request.QueryString("user")[i] + "<br>");
}
However, due to the nature of javascript objects the “in” statement does not just read
key properties, it also provides a means to access the inner methods of the javascript
“Request.QueryString()” object. As a result the first script will also produce results for the
“Count”, “Item” and “Key” methods.
This behaviour effectively cripples the use of enumeration with this script. However, there is a work-around
solution that still allows you to enumerate over the querystring or a given key but it produces script that is
longer than the second example above:
if (typeof Request.QueryString("user")[i] != "function") {
document.write(Request.QueryString("user")[i] + "<br>");
}
}