Created
January 23, 2017 16:20
-
-
Save superalsrk/0effb79f2aeb559b36b30fdfeb062b71 to your computer and use it in GitHub Desktop.
JS helpers
This file contains 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 UpdateQueryString(key, value, url) { | |
if (!url) url = window.location.href; | |
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"), | |
hash; | |
if (re.test(url)) { | |
if (typeof value !== 'undefined' && value !== null) | |
return url.replace(re, '$1' + key + "=" + value + '$2$3'); | |
else { | |
hash = url.split('#'); | |
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, ''); | |
if (typeof hash[1] !== 'undefined' && hash[1] !== null) | |
url += '#' + hash[1]; | |
return url; | |
} | |
} | |
else { | |
if (typeof value !== 'undefined' && value !== null) { | |
var separator = url.indexOf('?') !== -1 ? '&' : '?'; | |
hash = url.split('#'); | |
url = hash[0] + separator + key + '=' + value; | |
if (typeof hash[1] !== 'undefined' && hash[1] !== null) | |
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