Redirecting to a relative URL in JavaScript
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?
javascript url redirect
add a comment |
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?
javascript url redirect
add a comment |
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?
javascript url redirect
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?
javascript url redirect
javascript url redirect
edited Aug 20 '18 at 20:35
AnkDasCo
595314
595314
asked Oct 31 '09 at 17:43
user199337user199337
3,06371917
3,06371917
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
22
see why you should usewindow.location.replace
stackoverflow.com/questions/503093/…
– gideon
Dec 14 '10 at 5:32
42
When you want to simulate a link, you should usewindow.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).
– Benji XVI
Oct 13 '11 at 14:50
21
By the way,document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.
– Benji XVI
Oct 13 '11 at 14:53
6
I found usingwindow.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.
– Marcus Cunningham
Nov 8 '16 at 10:24
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
add a comment |
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.
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
add a comment |
redirect to ../
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
add a comment |
<a href="..">no JS needed</a>
..
means parent directory.
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
add a comment |
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level up
window.location.assign("/path");
// relative to domain
add a comment |
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'
add a comment |
protected by Community♦ Sep 23 '17 at 7:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
22
see why you should usewindow.location.replace
stackoverflow.com/questions/503093/…
– gideon
Dec 14 '10 at 5:32
42
When you want to simulate a link, you should usewindow.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).
– Benji XVI
Oct 13 '11 at 14:50
21
By the way,document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.
– Benji XVI
Oct 13 '11 at 14:53
6
I found usingwindow.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.
– Marcus Cunningham
Nov 8 '16 at 10:24
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
add a comment |
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
22
see why you should usewindow.location.replace
stackoverflow.com/questions/503093/…
– gideon
Dec 14 '10 at 5:32
42
When you want to simulate a link, you should usewindow.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).
– Benji XVI
Oct 13 '11 at 14:50
21
By the way,document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.
– Benji XVI
Oct 13 '11 at 14:53
6
I found usingwindow.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.
– Marcus Cunningham
Nov 8 '16 at 10:24
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
add a comment |
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
You can do a relative redirect:
window.location.href = '../'; //one level up
or
window.location.href = '/path'; //relative to domain
edited Nov 18 '18 at 17:24
answered Oct 31 '09 at 17:50
KobiKobi
107k34221264
107k34221264
22
see why you should usewindow.location.replace
stackoverflow.com/questions/503093/…
– gideon
Dec 14 '10 at 5:32
42
When you want to simulate a link, you should usewindow.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).
– Benji XVI
Oct 13 '11 at 14:50
21
By the way,document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.
– Benji XVI
Oct 13 '11 at 14:53
6
I found usingwindow.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.
– Marcus Cunningham
Nov 8 '16 at 10:24
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
add a comment |
22
see why you should usewindow.location.replace
stackoverflow.com/questions/503093/…
– gideon
Dec 14 '10 at 5:32
42
When you want to simulate a link, you should usewindow.location.href
. You should only usewindow.location.replace
when you want to simulate an http redirect (thus not generating a history item).
– Benji XVI
Oct 13 '11 at 14:50
21
By the way,document.location
was intended as a read-only property. It is safer to usewindow.location
. See this question.
– Benji XVI
Oct 13 '11 at 14:53
6
I found usingwindow.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.
– Marcus Cunningham
Nov 8 '16 at 10:24
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
22
22
see why you should use
window.location.replace
stackoverflow.com/questions/503093/…– gideon
Dec 14 '10 at 5:32
see why you should use
window.location.replace
stackoverflow.com/questions/503093/…– gideon
Dec 14 '10 at 5:32
42
42
When you want to simulate a link, you should use
window.location.href
. You should only use window.location.replace
when you want to simulate an http redirect (thus not generating a history item).– Benji XVI
Oct 13 '11 at 14:50
When you want to simulate a link, you should use
window.location.href
. You should only use window.location.replace
when you want to simulate an http redirect (thus not generating a history item).– Benji XVI
Oct 13 '11 at 14:50
21
21
By the way,
document.location
was intended as a read-only property. It is safer to use window.location
. See this question.– Benji XVI
Oct 13 '11 at 14:53
By the way,
document.location
was intended as a read-only property. It is safer to use window.location
. See this question.– Benji XVI
Oct 13 '11 at 14:53
6
6
I found using
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.– Marcus Cunningham
Nov 8 '16 at 10:24
I found using
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.– Marcus Cunningham
Nov 8 '16 at 10:24
2
2
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
@MarcusCunningham, in your example, the directory is /customers/ - so "one level up" is www.example.com/. Now if your example URL was www.example.com/customers/list/ - it would redirect you to www.example.com/customers/
– Ubeogesh
Jun 4 '18 at 11:54
add a comment |
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.
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
add a comment |
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.
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
add a comment |
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.
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.
edited Jan 6 '17 at 7:06
ygrichman
6,047167
6,047167
answered Oct 31 '09 at 17:49
BobBob
68.1k27107122
68.1k27107122
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
add a comment |
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
My browser barks when I go location.path and it seems only to recognize location.pathname. Hints?
– Konrad Viltersten
Jan 9 '16 at 20:19
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
location.path is incorrect, it should be location.hostname. I've added an edit to the post to fix this.
– ygrichman
Jan 6 '17 at 7:07
add a comment |
redirect to ../
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
add a comment |
redirect to ../
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
add a comment |
redirect to ../
redirect to ../
answered Oct 31 '09 at 17:48
Chris BallanceChris Ballance
23.4k2594144
23.4k2594144
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
add a comment |
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
2
2
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
Why the DV? Does this ever not work?
– Chris Ballance
Oct 31 '09 at 17:54
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
If your app is hosted in a sub-uri and the app doesn't know the sub-uri path. If you are at your apps root and you do ../ the app won't know how to get back to its root. For example, the same app is hosted at example.com/myapp and other.example.com/app2
– ReggieB
Jan 16 '14 at 9:29
add a comment |
<a href="..">no JS needed</a>
..
means parent directory.
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
add a comment |
<a href="..">no JS needed</a>
..
means parent directory.
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
add a comment |
<a href="..">no JS needed</a>
..
means parent directory.
<a href="..">no JS needed</a>
..
means parent directory.
answered Oct 31 '09 at 17:49
KornelKornel
76k28171233
76k28171233
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
add a comment |
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
11
11
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
That requires a click or some other user-initiated navigation.
– recursive
Sep 28 '12 at 22:44
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
Thanks for this. This saved time for me
– karthickj25
Feb 18 '18 at 8:46
add a comment |
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level up
window.location.assign("/path");
// relative to domain
add a comment |
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level up
window.location.assign("/path");
// relative to domain
add a comment |
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level up
window.location.assign("/path");
// relative to domain
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
window.location.assign("../");
// one level up
window.location.assign("/path");
// relative to domain
answered Dec 16 '17 at 3:31
Shaun LuttinShaun Luttin
59.2k33227287
59.2k33227287
add a comment |
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
edited Sep 22 '17 at 22:07
Krinkle
8,11642342
8,11642342
answered Sep 13 '17 at 18:04
Jorge Santos NeillJorge Santos Neill
1052
1052
add a comment |
add a comment |
protected by Community♦ Sep 23 '17 at 7:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?