Get QueryString using Javascript


QueryString is a requested pairs of key and value for URL. For example, in the URL http://www.sample.com/index.php?a=1&b=2&c=3 the QueryString is a=1&b=2&c=3. The first delimiter between the requested file name and the starting parameters is question mark while the parameters are separated by the ‘&’ character. The QueryString is the input parameters to the dynamic pages, therefore dynamic pages such as PHP, JSP or ASP.NET can process these parameters using similar GET or Request global variables to fetch these key pairs. In Client Side, the Javascript can analyze the QueryString as well using the following code snippet.

1
2
3
4
5
6
7
8
9
10
11
12
  function querystring(what)
  {
    var url = unescape(location.search);
    var Request = new Object();
    var str = url.substr(1);
    strs = str.split("&");
    for(var i = 0; i < strs.length; i++)
    {
      Request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
    return (Request[what]);
  }
  function querystring(what)
  {
    var url = unescape(location.search);
    var Request = new Object();
    var str = url.substr(1);
    strs = str.split("&");
    for(var i = 0; i < strs.length; i++)
    {
      Request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
    return (Request[what]);
  }

Instead of processing in the server side, the javascript just takes care of the HTML rendering/user interaction at the client side, which saves the bandwith. However, as Javascript may not be supported or disabled in some browsers, we cannot rely this method to process pages with important content e.g. banking. The other reason is that Javascript is visible to the users and may be hacked easily.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
267 words
Last Post: Javascript Page404 Colour Screen
Next Post: MSDOS .COM Assembly TV Colour Screen

The Permanent URL is: Get QueryString using Javascript

Leave a Reply