490

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
  • 7,536
  • 3
  • 37
  • 41
user199337
  • 8,063
  • 7
  • 23
  • 18

8 Answers8

907

You can do a relative redirect:

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

or

window.location.href = '/path'; //relative to domain
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 44
    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
  • 76
    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
  • 29
    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
  • 9
    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
  • 8
    @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
17

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
  • 7,087
  • 1
  • 10
  • 8
Bob
  • 97,670
  • 29
  • 122
  • 130
16

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
  • 133,272
  • 81
  • 405
  • 467
12

redirect to ../

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
  • 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
  • 97,764
  • 37
  • 219
  • 309
4

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
  • 10,032
  • 6
  • 34
  • 48
Jorge Santos Neill
  • 1,635
  • 13
  • 6
3

This is an old question but just to provide more information, this is how urls work:

window.location.href = 'https://domain/path';   // absolute
window.location.href = '//domain/path';         // relative to current schema
window.location.href = 'path';                  // relative to current path
window.location.href = '/path';                 // relative to domain
window.location.href = '../';                   // one level up
Teocci
  • 7,189
  • 1
  • 50
  • 48
2

try following js code

location = '..'
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345