0

Suppose I'm on this page:

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

I want to redirect the user to:

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

How can I do this?

(I'm also familiar with window.history.back(); but my question is something else!)

Thanks.

Sky
  • 4,244
  • 7
  • 54
  • 83
  • 1
    Read up on [String.split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split). – Mike Cluck Mar 27 '15 at 22:11
  • Have you ever saw this ? http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery – Daniel.V Mar 27 '15 at 22:13
  • @sky this-> http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery – Daniel.V Mar 27 '15 at 22:15
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – NinjaCat Mar 27 '15 at 22:17
  • @sky did you consider this before?window.location.replace("http://example.com/users/BECCA/edit") – Daniel.V Mar 27 '15 at 22:18
  • 1
    @Dan Looks like OP is searching for universal solution, like the one [I provided](http://stackoverflow.com/a/29311050). – nicael Mar 27 '15 at 22:20
  • @Ninja Not really a dupe; see the above comment. – nicael Mar 27 '15 at 22:24

2 Answers2

1
  1. Split your current location into an array using delimiter /
  2. Remove the last object in this array
  3. Join the objects into string taking / as delimiter

location.href=location.href.split("/").slice(0,-1).join("/");
nicael
  • 18,550
  • 13
  • 57
  • 90
0

The first answer is right but just have one problem, it does't work for http://example.com/
So the better code should be this:

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("/");
}
Daniel.V
  • 2,322
  • 7
  • 28
  • 58