Getting Querystring value using javascript

Kailashb87
Posted by Kailashb87 under JavaScript category on | Points: 40 | Views : 2146
This article contains code snippet for getting querystring value using javascript. This below javascript function retrieves query string value from url taking querystring name as parameter
Code Snippet

<script>  
function getParameterByName(name) {
url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>

Calling above function

This below javascript code calls above declared function for displya querystring value
<script>  
var vl=getParameterByName('id');
if(vl){
alert(vl)
}
</script>

Comments or Responses

Login to post response