Skip to content

Instantly share code, notes, and snippets.

@ahnbizcad
Last active September 9, 2015 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahnbizcad/5e96942e3224237c3530 to your computer and use it in GitHub Desktop.
Save ahnbizcad/5e96942e3224237c3530 to your computer and use it in GitHub Desktop.
function addOrReplaceQueryParam(url,param,value) {
var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
var paramCount = url.split("?").length - 1;
var separator = url.indexOf('?') !== -1 ? '&' : '?',
if (url.match(re))
{
return url.replace(re, '$1' + param + "=" + value + '$2');
}
else
{
return url + separator + param + "=" + value;
}
}
pdateQueryParam = function(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)(.*)", "gi");
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
return url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
return url.replace(re, '$1$3').replace(/(&|\?)$/, '');
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?',
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (hash[1]) url += '#' + hash[1];
return url;
}
else
return url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment