我的问题是,我想通过JavaScript重定向到上面的目录。

我的代码:

location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'))

URL看起来是这样的:

example.com/path/folder/index.php ?文件= abc&test = 123 lol =酷

重定向的影响如下:

example.com/path/&test = 123 lol =酷

但要有这个:

example.com/path/

我该怎么做呢?


当前回答

https://developer.mozilla.org/en-US/docs/Web/API/Location/assign

window.location.assign(“. . /”);//上一层 window.location.assign(" /路径”);//相对于域

其他回答

你可以做一个相对重定向:

window.location.href = '../'; //one level up

or

window.location.href = '/path'; //relative to domain

重定向到../

试试下面的js代码

Location = '..'

如果你使用位置。主机名你会得到你的domain.com部分。然后位置。Pathname会给你/path/folder。我会分割位置。pathname通过/和重新组装URL。但除非你需要查询字符串,你可以重定向到..转到上面的目录。

https://developer.mozilla.org/en-US/docs/Web/API/Location/assign

window.location.assign(“. . /”);//上一层 window.location.assign(" /路径”);//相对于域