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:


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 =|&/);

for(var i=0; i < parameters.length; i++) {

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]);
}); = '?' + list.join('&');
return parser.href;

Code to run the above function:

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: