494

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?

0

8 Answers 8

912

You can do a relative redirect:

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

or

window.location.href = '/path'; //relative to domain
5
  • 44
    see why you should use window.location.replace stackoverflow.com/questions/503093/…
    – gideon
    Dec 14, 2010 at 5: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, 2011 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.
    – Benji XVI
    Oct 13, 2011 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. Nov 8, 2016 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 4, 2018 at 11:54
18

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.

2
  • My browser barks when I go location.path and it seems only to recognize location.pathname. Hints? Jan 9, 2016 at 20:19
  • location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
    – ygrichman
    Jan 6, 2017 at 7:07
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
1
  • 1
    the "/path" doesnt work for assign. It would not include the domain but just try to open "/path"
    – Pumuckelo
    Aug 2, 2022 at 12:12
12

redirect to ../

1
  • 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 example.com/myapp and other.example.com/app2
    – ReggieB
    Jan 16, 2014 at 9:29
6

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

.. means parent directory.

1
  • 16
    That requires a click or some other user-initiated navigation.
    – recursive
    Sep 28, 2012 at 22:44
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'
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
2

try following js code

location = '..'

Not the answer you're looking for? Browse other questions tagged or ask your own question.