我在此站点上看到了许多URL拆分技术,但可以找到我所需的一种。
我需要砍掉最后一个参数。
即。
http://www.website.com/Pages/SearchResultsPage.aspx?k=website&cs=This%20Site&u=http%3A%2F%2Fwww.website.com%2FOur-Commitment
至
http://www.website.com/Pages/SearchResultsPage.aspx?k=website&cs=This%20Site
URL并不总是相同的,因此解决方案必须以某种方式搜索最后的“&”并将其剥离。
我设法做到了,但我希望减少行数:
rewriteURL();
function rewriteURL() {
var url = window.location;
var urlparts = url.split(/[\s&]+/);
var newURL = urlparts[0] + '&' + urlparts[1];
window.location.href = newURL;
}
最佳答案
function rewriteURL() {
window.location.href = window.location.href.substring(0, window.location.href.lastIndexOf('&'));
}