Javascript and Querystring Variables

Javascript

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Technorati
  • Yahoo! Bookmarks
  • E-mail this story to a friend!

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>