Skip to content

Instantly share code, notes, and snippets.

@solancer
Created August 11, 2016 15:06
Show Gist options
  • Save solancer/055fedc4b7778077b7c2ec88f4efa8e9 to your computer and use it in GitHub Desktop.
Save solancer/055fedc4b7778077b7c2ec88f4efa8e9 to your computer and use it in GitHub Desktop.
Javascript Params
function setURLParam(key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "i");
var newSearch = location.search;
// Is the key-value pair present in the existing search?
if (re.test(newSearch)) {
// Update a value
newSearch = newSearch.replace(re, '$1' + key + "=" + value + '$2$3');
} else {
// Add the key and its value
var separator = newSearch.indexOf('?') !== -1 ? '&' : '?';
newSearch = newSearch + separator + key + '=' + value;
}
// Reload the page with the new search
if (location.search === '') {
location.replace(location.href + newSearch);
} else {
location.replace(location.href.replace(location.search, newSearch));
}
}
function removeURLParam(key) {
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "i");
var newSearch = location.search;
if (re.test(newSearch)) {
newSearch = newSearch.replace(re, '$1$3').replace(/(&|\?)$/, '');
/* NOTE: The replace function of the location object is dissimilar to the
* replace function of the string object.
*/
location.replace(location.href.replace(location.search, newSearch));
} else {
console.log("removeURLParam unable to remove '" + key +"'; key not found.");
}
}
function getURLParam(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment