use*_*337 328 javascript url redirect
我有一个问题:我想通过JavaScript重定向到上面的目录.我的代码:
location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'));
Run Code Online (Sandbox Code Playgroud)
网址如下所示:
example.com/path/folder/index.php?file=abc&test=123&lol=cool
重定向会影响到这个:
example.com/path/&test=123&lol=cool
但想拥有这个:
example.com/path/
我怎么能这样做?
Kob*_*obi 632
你可以做一个相对重定向:
window.location.href = '../'; //one level up
Run Code Online (Sandbox Code Playgroud)
要么
window.location.href = '/path'; //relative to domain
Run Code Online (Sandbox Code Playgroud)
Bob*_*Bob 12
如果您使用,location.hostname
您将获得domain.com部分.然后location.pathname
会给你/ path /文件夹.我会拆分location.pathname
/并重新组合URL.但除非您需要查询字符串,否则您只需重定向..
即可转到上面的目录.
https://developer.mozilla.org/zh-CN/docs/Web/API/Location/assign
window.location.assign("../");
//上一层window.location.assign("/path");
//相对于域 归档时间: |
|
查看次数: |
446258 次 |
最近记录: |