是否可以在 url 中插入一个字符串?
假设我希望 www.domain.com/news
在 com 和 news 之间插入语言标志 de
最佳答案
如果您有一个包含协议(protocol)的完整 url 字符串,或者您知道基本 url,或者如果这全部基于当前 location
,您可以使用 URL
接口(interface)
const url = new URL('http://www.example.com/news');
url.pathname = '/de' + url.pathname;
console.log(url.href);
// using current page `location`
const pageurl = new URL(location.href);
pageurl.pathname = '/foobar' + pageurl.pathname;
console.log(pageurl.href);
关于javascript - 将字符串插入 url 并重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549756/