1

Possible Duplicate:
Get query string values in JavaScript

I need a function to read query string value?

If I give key then the method should return the corresponding value.

Please help..

Community
  • 1
  • 1
Codesen
  • 7,724
  • 5
  • 29
  • 31

3 Answers3

1

Try this will help

function fnGetValueByKey(key)
{
  key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + key + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Codesen
  • 7,724
  • 5
  • 29
  • 31
0

Ive allways used the following and it seems to work

function urlParam(name){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec (window.location.href);
    if (results == null)
        return "";
        else
        return results[1];  
}

You just need to call urlParam("yourName"); and it will return your value;

Dominic Green
  • 10,142
  • 4
  • 30
  • 34
  • Looks disturbingly close to this http://stackoverflow.com/a/901144/995876 – Esailija Aug 01 '12 at 11:11
  • Yer its commonly used every where, ive been using it for years but im sure I got it from a forum or something at some point, im for sure not clever enough to write it myself ;) – Dominic Green Aug 01 '12 at 11:12
0

yAccess desired query using getQuery('mykey');

function getQuery( key) {
        var hu = window.location.search.substring(1), gy = hu.split("&"), val;
        keyValue = {};
        for ( var i = 0; i < gy.length; i++) {
            val = gy[i].split("=");
            keyValue[val[0]] = val[1];
        }
        return keyValue[key];
    }
Anoop
  • 23,044
  • 10
  • 62
  • 76