The Request.QueryString(name) method returns a VB Collection object. This link defines the properties and methods of the Collection object. The main ones we are concerned with are Count and Item.
The Count property (as mentioned in my previous article) gives a count of the number of items it the collection and the Item property allows you to get at the individual items. Two thing should be noted about the Item property: first that it is a method not an array and second that it is one based not zero based, so the first item is retrieved using Item(1) not Item[0].
For example, if your QueryString contains "widget=abc&widget=xyz" and you do:
var widget = Response.QueryString("widget");
widget.Count // 2
widget.Item(1) // "abc"
widget.Item(2) // "xyz"
Wednesday, 21 December 2011
Tuesday, 29 November 2011
Checking QueryString in ASP using JavaScript
The QueryString object appears to act in a very odd manner when using JavaScript as your ASP language. The main problem with it is that you cannot check to see if a query string parameter is missing using undefined; it simply does not work.
As an example, say your script has been called with no parameter; now check to see if the parameter name is present in the QueryString object like this: if (Request.QueryString("name") == undefined) {...} - this will evaluate to false, even though name has not been defined. It also does not evaluate to null, 0, or the empty string.
The only way I have been able to discover to evaluate this is to use the Count method. for example:
var myvar = Request.QueryString("name");
if (myvar.Count === 0) {myvar = ""; }
This will work correctly.
As an example, say your script has been called with no parameter; now check to see if the parameter name is present in the QueryString object like this: if (Request.QueryString("name") == undefined) {...} - this will evaluate to false, even though name has not been defined. It also does not evaluate to null, 0, or the empty string.
The only way I have been able to discover to evaluate this is to use the Count method. for example:
var myvar = Request.QueryString("name");
if (myvar.Count === 0) {myvar = ""; }
This will work correctly.
Subscribe to:
Posts (Atom)