admin管理员组文章数量:1022705
I have this code:
$(window).ready(function() {
var url = window.location.href;
if (url.includes("#/projet/")) {
projectId = url.substring(url.indexOf("#")+1).split("/").slice(2, 3).toString();
window.location.href = "projects/" + projectId;
};
})
I'm redirected but the window.location is not replaced, just concatenated.
For instance, if my URL is localhost:3000/users/212323/dashboard
, after the javascript redirection, I get localhost:3000/users/212323/projects/123456
instead of localhost:3000/projects/123456
I don't understand why the href is concatenated and not replaced, do you have an idea?
I have this code:
$(window).ready(function() {
var url = window.location.href;
if (url.includes("#/projet/")) {
projectId = url.substring(url.indexOf("#")+1).split("/").slice(2, 3).toString();
window.location.href = "projects/" + projectId;
};
})
I'm redirected but the window.location is not replaced, just concatenated.
For instance, if my URL is localhost:3000/users/212323/dashboard
, after the javascript redirection, I get localhost:3000/users/212323/projects/123456
instead of localhost:3000/projects/123456
I don't understand why the href is concatenated and not replaced, do you have an idea?
Share Improve this question asked Dec 18, 2018 at 8:51 cercxtrovacercxtrova 1,67115 silver badges35 bronze badges 2-
4
You need
"/projects/"
, not"projects/"
. – Ry- ♦ Commented Dec 18, 2018 at 8:52 - Perfect, thank you @Ry- – cercxtrova Commented Dec 18, 2018 at 8:55
2 Answers
Reset to default 6window.location.href = 'someurl'
works the same way as clicking that someurl
in a <a>
tag.
When using a relative path (i.e. without /
in the beginning), your browser will concatenate the URL to the existing URL.
Simple fix in your case is to prepend the /
:
window.location.href = "/projects/" + projectId;
Note though, that this will cause the site possibly not work anymore if it is moved to another location. That is why many web frameworks use full URLs and some kind of base-url to get the linking correctly.
You need to add another /
to the beginning of the url, otherwise the browser interprets the url as a relative url to the curent url.
window.location.href = "/projects/" + projectId;
The extra /
at the start tells the browser to start from the root url.
I have this code:
$(window).ready(function() {
var url = window.location.href;
if (url.includes("#/projet/")) {
projectId = url.substring(url.indexOf("#")+1).split("/").slice(2, 3).toString();
window.location.href = "projects/" + projectId;
};
})
I'm redirected but the window.location is not replaced, just concatenated.
For instance, if my URL is localhost:3000/users/212323/dashboard
, after the javascript redirection, I get localhost:3000/users/212323/projects/123456
instead of localhost:3000/projects/123456
I don't understand why the href is concatenated and not replaced, do you have an idea?
I have this code:
$(window).ready(function() {
var url = window.location.href;
if (url.includes("#/projet/")) {
projectId = url.substring(url.indexOf("#")+1).split("/").slice(2, 3).toString();
window.location.href = "projects/" + projectId;
};
})
I'm redirected but the window.location is not replaced, just concatenated.
For instance, if my URL is localhost:3000/users/212323/dashboard
, after the javascript redirection, I get localhost:3000/users/212323/projects/123456
instead of localhost:3000/projects/123456
I don't understand why the href is concatenated and not replaced, do you have an idea?
Share Improve this question asked Dec 18, 2018 at 8:51 cercxtrovacercxtrova 1,67115 silver badges35 bronze badges 2-
4
You need
"/projects/"
, not"projects/"
. – Ry- ♦ Commented Dec 18, 2018 at 8:52 - Perfect, thank you @Ry- – cercxtrova Commented Dec 18, 2018 at 8:55
2 Answers
Reset to default 6window.location.href = 'someurl'
works the same way as clicking that someurl
in a <a>
tag.
When using a relative path (i.e. without /
in the beginning), your browser will concatenate the URL to the existing URL.
Simple fix in your case is to prepend the /
:
window.location.href = "/projects/" + projectId;
Note though, that this will cause the site possibly not work anymore if it is moved to another location. That is why many web frameworks use full URLs and some kind of base-url to get the linking correctly.
You need to add another /
to the beginning of the url, otherwise the browser interprets the url as a relative url to the curent url.
window.location.href = "/projects/" + projectId;
The extra /
at the start tells the browser to start from the root url.
本文标签: javascriptwindowlocation not replaced but concatenatedStack Overflow
版权声明:本文标题:javascript - window.location not replaced but concatenated - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745478112a2152415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论