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

我该怎么做呢?


当前回答

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

其他回答

重定向到../

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

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

or

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

我试图将我当前的网站重定向到同一页面上的其他部分,使用JavaScript。以下代码为我工作:

location.href='/otherSection'

试试下面的js代码

Location = '..'

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