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?
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
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.
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../"); // one level up
window.location.assign("/path"); // relative to domain
redirect to ../
<a href="..">no JS needed</a>
..
means parent directory.
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'
try following js code
location = '..'
Success story sharing
window.location.replace
stackoverflow.com/questions/503093/…window.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.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.