Alex Rivera | Logout

Redirecting to a relative URL in JavaScript

Asked 2009-10-31T17:43:53.627
512

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?

Edit
Report

1 Answer

948

You can do a relative redirect:

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

or

window.location.href = '/path'; //relative to domain
answered 2009-10-31T17:50:09.747

Your Answer