article

read querystring values in js

[WHAT]

  1. ] The SO article referenced below(#1) indicates several methods 1 can use to get/read querystring parameters values ...
    1. ] the top rated and 'accepted as answer' method using vanilla js may not be the best answer, here are a couple of the others answers in the article
    2. ] i like the OO approach by __ for the way it enables you to access the param values qs["paramater"], but IMO the codeblock is somewhat(obsfuc,unreadable) 
    3. ] i used a jq function submitted by community wiki based on work by Roshambo , see exa 2 below
    4. ] it also points to a jq plugin that will do this, as well as several other answers(more jq plugin solutions) 
    5. ] 1 answer shows the 'obsfucated' code that google uses
    6. ] 1 answer shows a performance analysis of using jq plugin code VS plain vanilla js code

[WHY]

  1. ] reading values passed in a querystring is something you will commonly do

[WHERE]

  1. ] everywhere

[WHEN]

  1. ] almost always

[EXAMPLE]

  1. var qs = (function(a) {
  2.     if (a == "") return {};
  3.     var b = {};
  4.     for (var i = 0; i < a.length; ++i)
  5.     {
  6.         var p=a[i].split('=');
  7.         if (p.length != 2) continue;
  8.         b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
  9.     }
  10.     return b;
  11. })(window.location.search.substr(1).split('&'));

  1. $.urlParam = function(name, url){
  2.     if(!url){
  3.      url = window.location.href;
  4.     }
  5.     var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
  6.     if (!results)
  7.     { 
  8.         return 0; 
  9.     }
  10.     return results[1] || 0;
  11. }

[HOW-TO]

  1. ] example 1
    1. ] var paramValue = qs["paramName"] - this returns the value that you want to read
  2. ] example 2
    1. ] var paramValue = $.urlParam(paramName,url)

[REFERENCE]

  1. ] http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
  2. ] http://snipplr.com/view/26662/get-url-parameters-with-jquery--improved/ by 
  3. ] http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

 

Details Photos Edit more

Details

ID: 2245

NAME: how-to-read-querystring-values

DESCRIPTION: an example of how-to read querystring values in your client side code using javascript

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: 0

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and