Reading querystring using javascript

Hi while searching for the same,

got the following javascript

q = location.search;

getParam = function(arg) {
if (q.indexOf(arg) >= 0) {
var pntr = q.indexOf(arg) + arg.length + 1;
if (q.indexOf(‘&’, pntr) >= 0) {
return q.substring(pntr, q.indexOf(‘&’, pntr));
} else {
return q.substring(pntr, q.length);
}
} else {
return null;
}
}

var objectId = getParam(‘oId’);

alert(objectId);

or better

function querySt(ji) {
hu = window.location.search.substring(1);
gy = hu.split(“&”);
for (i=0;i<gy.length;i++) {
ft = gy[i].split(“=”);
if (ft[0] == ji) {
return ft[1];
}
}
}

var pId= querySt(“pId”);
alert(pId);

http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml

Here oId is the query string parameter whose value we need.

This works even if there are multiple query string parameters !

Bye…


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

4 thoughts on “Reading querystring using javascript”

  1. Sorry not trying to spam, but I’m messing with my blog and broke the link… Here’s the code:

    var str=”http://www.google.com?q=blah&vv=ha&test&nah=yeah”; // window.location.href
    var regex = /[?&]([^=&]*)(?:=([^&]*))?/g;
    var res, params = {};
    while ((res = regex.exec(str)) != null)
    {
    params[res[1]] = res[2] || “n/a”;
    }

    for (a in params)
    {
    document.write(a + “=” + params[a] + “”);
    }

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s