Created
April 1, 2014 12:07
-
-
Save anton-rudeshko/9912630 to your computer and use it in GitHub Desktop.
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 replaceUrl(key, newValue) { | |
var url = window.location.href, | |
separator = ~url.indexOf('?') ? '&' : '?', | |
re = new RegExp('([?&])' + key + '=(.*?)([?&]|$)', 'i'), | |
match = re.exec(url), | |
hasParam = !!match; | |
if (hasParam) { | |
if (match[2] === newValue || !newValue) { | |
return url.replace(re, ''); | |
} | |
return url.replace(re, '$1' + key + '=' + newValue + '$3'); | |
} | |
return url + separator + key + '=' + (newValue || '1'); | |
} | |
function toggle(key, newValue) { | |
window.location.href = replaceUrl(key, newValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment