fbpx

A simple function to easily retrieve GET parameters by name.
Example:
for the URL: http://www.example.com/?prodID=1234

var prodId = getParameterByName('prodId');

Sets the javascript variable “prodID” to “1234” as defined on the GET string.

/* 
 * function getParameterByName(name)
 * returns value defined in URL parameter
 */
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, " "));
}