12-08-2009

Javascript Retrieve get name-value pairs from url

This simple regex based function returns a js object built from the value name pairs in the get string allowing you to access them in a manner similar to the PHP $_GET global.

If necessary adjust the regular expression to your needs, but this should fit most general purposes:

urlStringGet=function(){
	var rv = {},s=[];
	var gets = location.href.match(/[a-zA-Z0-9-_]+=[^&]+/g);
	if (gets){
		var l = gets.length; 
		for(var i =0; i<l; i++){
			s = gets[i].split('='); 
			rv[s[0]]=s[1];
		}
	}
	return rv; 
}

Comments:

Your comment:

»

 

[x]