我的问题是,我想通过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/

我该怎么做呢?


当前回答

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

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

or

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

其他回答

< a href = " . .>没有JS需要</a>

. .表示父目录。

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

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

or

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

试试下面的js代码

Location = '..'

重定向到../

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