在Javascript中重定向到以前的ROUTE

时间:2015-03-27 22:10:39

标签: javascript jquery html redirect

假设我在这个页面上:

http://example.com/users/BECCA/edit/advanced

我想将用户重定向到:

http://example.com/users/BECCA/edit

我该怎么做?

(我也熟悉window.history.back();,但我的问题还不错!)

感谢。

2 个答案:

答案 0 :(得分:1)

  1. 使用分隔符/
  2. 将当前位置拆分为数组
  3. 删除此数组中的最后一个对象
  4. 将对象加入到以/作为分隔符
  5. 的字符串中
    location.href=location.href.split("/").slice(0,-1).join("/");
    

答案 1 :(得分:0)

第一个答案是正确的,但只有一个问题,它确实适用于http://example.com/
所以更好的代码应该是这样的:

var url = location.href.split("/");
if(url.lenght == 3){
//the url is http://example.com/ do what ever you want
}
else{
     location.href=url.slice(0,-1).join("/");
}