function replaceUrlParams(key, val) { let href = location.href; if (href.indexOf("?") === -1) return; var query = href.indexOf("?") + 1; var hash = href.indexOf("#") + 1; var first = href.substring(query, hash === 0 ? href.length : hash - 1); var sec = hash === 0 ? "" : href.substring(hash); first = first .split("&") .map(function (param) { return param.startsWith(key + "=") ? key + "=" + val : param; }) .join("&"); history.replaceState( "", "", href.substring(0, query) + first + (hash === 0 ? "" : "#" + sec) ); }
标签:hash,url,js,substring,href,var,query,替换,first From: https://www.cnblogs.com/laremehpe/p/17965025