For URL /foo/bar?param=5
the code changeUrlParam('param', 6)
will redirect to /foo/bar?param=6
For URL /foo/bar?param=5
the code changeUrlParam('param', null)
will redirect to /foo/bar
Last active
December 22, 2015 08:18
-
-
Save Znarkus/6444017 to your computer and use it in GitHub Desktop.
Change a query parameter in the current URL.
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 changeUrlParam(paramName, newValue) { | |
var search = location.search.replace(new RegExp('[?&]' + paramName + '=[^&]+'), ''), | |
url = location.pathname + search; | |
if (newValue !== null) { | |
url += (search ? '&' : '?') + | |
paramName + '=' + newValue; | |
} | |
location = url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment