11/15/07
Javascript and Querystring Variables
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.