View update-url-query-string-history.js
// Explicitly save/update a url parameter using HTML5's replaceState().
function updateQueryStringParam(key, value) {
const baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
const urlQueryString = document.location.search;
let newParam = key + '=' + value;
let params = '?' + newParam;
// If the "search" string exists, then build params from it
if (urlQueryString) {
const keyRegex = new RegExp('([\?&])' + key + '[^&]*');