460

My problem is that I want to redirect via JavaScript to a directory above.

My code:

location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'))

The URL looks like this:

example.com/path/folder/index.php?file=abc&test=123&lol=cool

The redirect affect just this:

example.com/path/&test=123&lol=cool

But want to have this:

example.com/path/

How can I do it?

Gass
  • 4,098
  • 2
  • 13
  • 27
user199337
  • 7,763
  • 7
  • 22
  • 18

7 Answers7

852

You can do a relative redirect:

window.location.href = '../'; //one level up

or

window.location.href = '/path'; //relative to domain
Kobi
  • 130,553
  • 41
  • 252
  • 283
  • 42
    see why you should use `window.location.replace` http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery/506004#506004 – gideon Dec 14 '10 at 05:32
  • 70
    When you want to simulate a link, you should use `window.location.href`. You should only use `window.location.replace` when you want to simulate an http redirect (thus not generating a history item). – Benji XVI Oct 13 '11 at 14:50
  • 28
    By the way, `document.location` was intended as a read-only property. It is safer to use `window.location`. See [this question](http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascript). – Benji XVI Oct 13 '11 at 14:53
  • 8
    I found using `window.location.href = '../'` redirected to the root of the site and not "one level up" as expected. When the current page is "www.example.com/customers/list" I needed to use `'./'`. I guess this is because "list" is not considered as a directory level. – Marcus Cunningham Nov 08 '16 at 10:24
  • 4
    @MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/ – Ubeogesh Jun 04 '18 at 11:54
16

If you use location.hostname you will get your domain.com part. Then location.pathname will give you /path/folder. I would split location.pathname by / and reassemble the URL. But unless you need the querystring, you can just redirect to .. to go a directory above.

ygrichman
  • 6,917
  • 1
  • 9
  • 8
Bob
  • 93,968
  • 29
  • 116
  • 128
14

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

  • window.location.assign("../"); // one level up
  • window.location.assign("/path"); // relative to domain
Shaun Luttin
  • 121,071
  • 74
  • 369
  • 447
11

redirect to ../

Chris Ballance
  • 32,977
  • 25
  • 102
  • 150
  • 1
    If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at http://example.com/myapp/ and http://other.example.com/app2/ – ReggieB Jan 16 '14 at 09:29
6

<a href="..">no JS needed</a>

.. means parent directory.

Kornel
  • 94,425
  • 32
  • 211
  • 296
3

I'm trying to redirect my current web site to other section on the same page, using JavaScript. This follow code work for me:

location.href='/otherSection'
Timo Tijhof
  • 9,867
  • 6
  • 33
  • 47
Jorge Santos Neill
  • 1,375
  • 9
  • 6
2

try following js code

location = '..'
Kamil Kiełczewski
  • 71,169
  • 26
  • 324
  • 295