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

其他回答

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

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

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

location.href='/otherSection'

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

. .表示父目录。

这是一个老问题,但只是为了提供更多信息,这是url的工作原理:

window.location.href = 'https://domain/path';   // absolute
window.location.href = '//domain/path';         // relative to current schema
window.location.href = 'path';                  // relative to current path
window.location.href = '/path';                 // relative to domain
window.location.href = '../';                   // one level up

试试下面的js代码

Location = '..'