Thursday, January 10, 2008

Getting Query String Values In JavaScript

There are many ways to get the query string values (name-value pairs) out of the URL (location) of the current page. Many examples use the String::split() method to split the location on the "?", the "&", and the "=". This keeps breaking the URL up into smaller arrays that can eventually be used to grab the query string name-value pairs.


Here I am showing different method for Getting Query String Values In JavaScript.


var objURL = new Object();
window.location.search.replace(new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),function( $0, $1, $2, $3 ){objURL[ $1 ] = $3;});



For Accessing above variable write this function which is written below :-

for (var strKey in objURL)
{
var myVariable = objURL[ strKey ];
}


This function is very nice for Getting Query String Values In JavaScript.

Thanks
Manoj Azad.

No comments: