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

Amassed 8 years of experience in the information technology field. Extensive hands-on skills attained in a wide range of technologies. Proven ability of delivering fast, accurate results. Worked in various capacities in the information technology field including: programmer, programmer analyst, technical analyst and quality assurance specialist.

Leave a Reply

Follow me on Twitter

-->
%d bloggers like this: