Monday 31 March 2014

JavaScript get query string values

Introduction:

Here I will explain how to get Query String value from URL in JavaScript.

Code:
function getParameterByName(name) {
 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
 results = regex.exec(location.search);
 return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


Example:





Suppose you have three Query String in current URL a b and c and you need value of b so just need to pass b in this function.

var b = getParameterByName('b');
alert(b);

This will alert value of b.



No comments :

Post a Comment