Getting the URL variables can be using when writing AJAX applications, here is a little snippet of a function.

Method Detail:

String[] getURLVars(url)

Parameters:

url – string e.g. http://hejp.co.uk?name=joe&sister=sally

Returns:

An associative array containing the URL variables.

Code:

function getURLVars(url){
	var urlStr = new String(url);
	var vars = new Object();
	var tempArr = new Array();

	urlStr = urlStr.substring(urlStr.indexOf('?')+1);
	tempArr = urlStr.split('&');

	for(var i=0;i<tempArr.length;i++){
		var equalsPos = tempArr[i].indexOf('=');
		var key = tempArr[i].substring(0, equalsPos),	value = unescape(tempArr[i].substring(equalsPos+1));
		vars[key] = value;
	}

	return vars;
}

Example:

/* window.location is the url of 'this' page */
var vars = getURLVars(window.location); // e.g. window.location = 'http://hejp.co.uk?name=joe&sister=sally'

document.write(vars['name'] + '<br/>' + vars['sister']);
/* Prints:
      joe
      sally
*/