Thursday, July 24, 2014

Get query string values in JavaScript?




Here is a simple function made using RegEx to get Query String values from URL in JavaScript.


// Function ::
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, " "));
}
// Usage ::
var queryVar = getParameterByName('key');

Related Posts Plugin for WordPress, Blogger...