RSS

Javascript and Querystring Variables

Thu, Nov 15, 2007

Web Development

In web browsers you can access the querystring with client-side JavaScript, but there is no standard way to parse out the name/value pairs. Andrew Urquhart from andrewu.co.uk has come up with a very useful Javascript (CSJSRequestObject) that mimics ASP’s server-side Request.QueryString() command.

Here is how it works:

1. Download CSJSRequestObject.js from http://andrewu.co.uk/tools/request/

2. Upload the script to your site and attach the js to your pages:

<head>
    <title>Your Page Title</title>
    <script type="text/javascript" src="CSJSRequestObject.js">
    </script>
</head>
 

3. Now you can access the variables in your querystring - say we are using the following querystring: http://www.yoursite.com/index.php?firstname=Bubba&lastname=Smith

<script type="text/javascript">
    var fname = Request.QueryString("firstname");
    alert("Hello " + fname + "!");
</script>
 

The script would return an alert box saying “Hello Bubba!”

This is the most basic of operations for this script. Check out all the capabilities here.

,

This post was written by:

Dustin - who has written 25 posts on DustinsDesign.com.


Contact the author

Leave a Reply