Reading query parameters using Java Script

Hi,

One of the best mechanism passing data between pages is query strings in the URL. As we know server side scripts like JSP,SERVLETS,ASP,PHP supports very easy way to parse query paremeters,

But  Java script being a client side script also supports a very easy way to parse the query parameters in the URL.

Use the below method and see the how easy we achived.

<script>

var url = window.location.toString(); // read the URL
url.match(/\?(.+)$/);
var prms = RegExp.$1;

var prms = prms.split(“&”); //SPLIT THE VALUES IN ARRAY
var stringList = {};

for(var i=0;i<prms.length;i++){
var tmpKey = prms[i].split(“=”);
stringList[tmpKey[0]] = unescape(tmpKey[1]);
}

for(var j in stringList){
document.write(j+” = “+stringList[j]+”<br/>”);
}
</script>

Thanks

R Vashi

Leave a Reply

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