View Single Post
  #2 (permalink)  
Old June 18th, 2010, 04:15 PM
Old Pedant Old Pedant is offline
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Use JavaScript to create the equivalent collection for use by other JS code:
Code:
<script type="text/javascript">
var querystring = [ ];
var qs = location.search;
if ( qs.length > 1 )
{
    qs = qs.substring(1); // skip past the ?
    var pairs = qs.split( /\&/g ); // get all the name=value pairst
    for ( var p = 0; p < pairs.length )
    {
        var pair = pairs[p];
        querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
    }
}
</script>
Then, anyplace in your page where in ASP code you might use
Code:
     var foo = Request.QueryString("foo");
you instead simply do
Code:
    var foo = querystring["foo"];
CAUTION: "foo" will be case sensitive, unlike in ASP. If you wish, you could replace
Code:
        querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
with
        querystring[ pair[0].toLowerCase() ] = unescape( pair[1].replace(/\+/g," ");
and then always use lower case names ("foo" in place of "Foo" or "FOO") when finding values.

Untested, though I have used this same code before. If there's a goof, it's just a typo.
Reply With Quote