views:

701

answers:

4

Hello,

I have a problem: I want to redirect via JavaScript to a directory above. My code:

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

The url looks like this:

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

The redirect affect just this:

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

But want to have this:

domain.com/path/

How could I do that?

+4  A: 

redirect to ../

Chris Ballance
Why the DV? Does this ever not work?
Chris Ballance
+3  A: 

If you use location.path 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.

Bob
+1  A: 

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

.. means parent directory.

porneL
+2  A: 

You can do a relative redirect:

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

or

document.location.href = '/path'; //relative to domain
Kobi