不确定这是否不合适,尽管它工作正常。
为了移动到新位置并保留根 URL,我做了如下示例:
window.location = window.location.href.split('/oldbar')[0] + '/foobar/' + $value
window.location = window.location.href.split(/oldbar/)[0] + 'foobar';
window.location = window.location.href.split('/oldbar')[0] + '/foobar/'
你会采取不同的做法吗?如果是这样,如何/为什么?
最佳答案
当您只需访问location object的pathname
属性时,您不需要拆分href。 :
// not really sure what you're trying to do, but...
window.location.pathname = window.location.pathname + "/foobar/";
关于javascript - 有人会比我更好地重构 URL 更改吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636028/