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?
This question is related to
javascript
url
redirect
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'
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level upwindow.location.assign("/path");
// relative to domaintry following js code
location = '..'
_x000D_
redirect to ../
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.
<a href="..">no JS needed</a>
..
means parent directory.
Source: Stackoverflow.com