Last active
September 9, 2015 02:08
-
-
Save ahnbizcad/5e96942e3224237c3530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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