Javascript – Redirecting to a relative URL in JavaScript

javascriptredirecturl

I have a problem:
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 could I do that?

Best Answer

You can do a relative redirect:

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

or

window.location.href = '/path'; //relative to domain