我想更改网址的路径名,我试过但是完整的网址正在修改。我需要url的最后一个值才需要更改。
这是我所期待的:
当前网址:localhost:8080/abc/index.html
期待网址:localhost:8080/abc/test.html
这就是我的尝试:
window.location.pathname = window.location.pathname.substring(window.location.pathname.lastIndexOf('/')+1);
仅返回localhost:8080/test.html
答案 0 :(得分:1)
尝试这样的事情:
var parts = window.location.href.split("/");
if (parts[parts.length - 1].length < 1) {
parts = parts.splice(parts.length - 1, 1);
}
parts[parts.length - 1] = 'test.html';
parts = parts.join("/");
console.log(parts);
答案 1 :(得分:1)
看起来你几乎拥有你想要的东西,请查看:
var pathname = "localhost:8080/abc/index.html";
var lastItem = pathname.substring(pathname.lastIndexOf('/')+1);
alert('Last item: ' + lastItem);
var startPart = pathname.substring(0, pathname.lastIndexOf('/')+1);
alert('Start part: ' + startPart);
var newUrl = startPart + 'test.html';
alert('New url: ' + newUrl);
&#13;