Javascript: Get URL Parameters

      No Comments on Javascript: Get URL Parameters

In you have to get parameters from url in javascript you can use the following function. It will return a parameter value based on the parameter name. You can modify this code to achieve similar tasks like iterating through all the parameters, counting or listing them:


<script type="text/javascript">
function getParamValue(parameter) 
{ 
	var urlParamString = location.search.substring(1, location.search.length);

	var paramValue = false;
	var params = urlParamString.split("&");
	
	for (i=0; i<params.length;i++) 
	{
		paramName = params[i].substring(0,params[i].indexOf('='));
		if (paramName == parameter) 
		{
			paramValue = params[i].substring(params[i].indexOf('=')+1)
		}
		
	}
	
	if (paramValue) 
	{
		return paramValue;
	}
	else 
	{
		return null;
	}
}

alert(getParamValue('url'));
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *