我的问题是,我想通过JavaScript将重定向到上一级目录。
我的代码:
location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'))
这个URL看起来像这样:
example.com/path/folder/index.php?file=abc&test=123&lol=cool
重定向只影响这个部分:
example.com/path/&test=123&lol=cool
但是想要的是这个:
example.com/path/
我该怎么做?
window.location.replace
请参考:https://dev59.com/_3RB5IYBdhLWcg3wz6Wd#506004 - gideonwindow.location.href
。只有当您想模拟一个 HTTP 重定向(因此不会生成历史记录项)时,才应该使用window.location.replace
。 - Benji XVIwindow.location.href = '../'
会将页面重定向到站点根目录,而不是像预期的那样“上一级”目录。 当前页面为“www.example.com/customers/list”时,我需要使用'./'
。 我猜这是因为“list”不被视为一个目录级别。 - Marcus Cunningham