JavaScript: Appending a parameters to url and refreshing the page.

First off this is not my code but I really wanted to share because it works great ツ

Hats off to Yeyo: http://stackoverflow.com/a/27970946

 

Add the bellow code in a script tag:

function URL_add_parameter(url, param, value){
var hash = {};
var parser = document.createElement('a');

parser.href = url;

var parameters = parser.search.split(/\?|&/);

for(var i=0; i < parameters.length; i++) {
if(!parameters[i])
continue;

var ary = parameters[i].split('=');
hash[ary[0]] = ary[1];
}

hash[param] = value;

var list = [];
Object.keys(hash).forEach(function (key) {
list.push(key + '=' + hash[key]);
});

parser.search = '?' + list.join('&');
return parser.href;
}

Code to run the above function:

window.location.href = URL_add_parameter(window.location.href, 'param', 'value');

 

Written by Paul

With 10 years of software development and support experience in the information technology industry, Paul has a proven track record in delivering swift, accurate results on multiple client engagements. He has developed cutting-edge web and client/server applications, E-Commerce customizations, and industry-specific solutions for clients. Currently working at the Government of...
Read more

Leave a Reply

-->
%d bloggers like this: