Skip to content

Instantly share code, notes, and snippets.

@benmann
Created March 1, 2017 10:19
Show Gist options
  • Save benmann/75788bd1d98ac18c4c9c043a506599f1 to your computer and use it in GitHub Desktop.
Save benmann/75788bd1d98ac18c4c9c043a506599f1 to your computer and use it in GitHub Desktop.
addOrUpdateUrlParam: function(uri, paramKey, paramVal) {
var regex = new RegExp("([?&])" + paramKey + "=[^&#]*", "i");
if (regex.test(uri)) {
uri = uri.replace(regex, '$1' + paramKey + "=" + paramVal);
} else {
var separator = /\?/.test(uri) ? "&" : "?";
uri = uri + separator + paramKey + "=" + paramVal;
}
if (history.pushState) {
var querystring = uri;
history.pushState(null, null, querystring);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment