Created
August 17, 2017 15:15
-
-
Save DominikAngerer/b5c874779000201e457fc2d8aab145dc to your computer and use it in GitHub Desktop.
Add/Update/Delete an URL Parameter without reload - History API
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
export function updateQueryStringParam(key, value) { | |
let baseUrl = [location.protocol, '//', location.host, location.pathname].join(''); | |
let urlQueryString = document.location.search; | |
let newParam = key + '=' + value; | |
let params = '?' + newParam; | |
// If the "search" string exists, then build params from it | |
if (urlQueryString) { | |
let updateRegex = new RegExp('([\?&])' + key + '[^&]*'); | |
let removeRegex = new RegExp('([\?&])' + key + '=[^&;]+[&;]?'); | |
if( typeof value == 'undefined' || value == null || value == '' ) { | |
// Remove param if value is empty | |
params = urlQueryString.replace(removeRegex, "$1"); | |
params = params.replace( /[&;]$/, "" ); | |
} else if (urlQueryString.match(updateRegex) !== null) { | |
// If param exists already, update it | |
params = urlQueryString.replace(updateRegex, "$1" + newParam); | |
} else { | |
// Otherwise, add it to end of query string | |
params = urlQueryString + '&' + newParam; | |
} | |
} | |
// no parameter was set so we don't need the question mark | |
params = params == '?' ? '' : params; | |
window.history.replaceState({}, "", baseUrl + params); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment