在JavaScript中重定向到一个相对的URL
我有一个问题。 我想通过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/
我怎样才能做到这一点?
325
3
如果你使用
location.hostname
,你将得到你的域名.com部分。然后location.pathname
会给你/path/folder。我将用/分割location.pathname
,然后重新组合URL。但是,除非你需要querystring,否则你可以直接重定向到...
去一个目录上面。重定向到".../"。
<a href=".">不需要JS</a>
。...
表示父目录。