Marius van Witzenburg We fight for our survival, we fight!

12Jun/110

How to get parameters from query string with JavaScript

I don't remember where I found this piece of code, but it works nice to get query string parameters :-)

function getURLParam(strParamName) {
	var strReturn = '';
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++) {
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}

it works quite simple, first it grabs all data after the question mark in the url, then it splits this result on the & character and then it splits again on the equal character. The result of that exists out of two pieces, the first is the key and the second the value of that key.

Posted by mariusvw

Reacties (0) Trackbacks (0)

Nog geen reacties


Leave a comment

(required)

Nog geen trackbacks.