简体   繁体   English

Javascript:转到根网址(以避免相对网址)

[英]Javascript: Going to root url (to avoid relative url)

I'm having an issue with urls.我的网址有问题。

I have a navbar.ejs file that I include in every page of my website via我有一个 navbar.ejs 文件,通过它包含在我网站的每个页面中

<% include ./navbar %>

In the file, I have something like this:在文件中,我有这样的东西:

<li><a id="loginButton" href="../login">Login</a></li>
<li><a id="signupButton" href="../users/new">Sign up</a></li>

The issue I'm having is that depending on where I am on the website, the login button might or might not work.我遇到的问题是,根据我在网站上的位置,登录按钮可能会或可能不会工作。 This is because of这是因为

"../login" “../登录”

So at times when you click the login button, you're directed to https://baseurl/login因此,有时当您单击登录按钮时,您会被定向到https://baseurl/login

At other times, it goes to https://baseurl/campgrounds/login This URL is wrong and doesn't exist.在其他时候,它会转到https://baseurl/campgrounds/login这个 URL 是错误的,不存在。

I know the source of the problem obviously.我显然知道问题的根源。 But how do I get replace "../login" with an absolute URL path... As in, how do I go back to the root?但是我如何用绝对 URL 路径替换“../login”...如,我如何回到根目录?

Something like $("#loginButton).attr("href", document.location.hostname + "/login") doesn't work because it just directs to baseurl/baseurl/login which is also invalid像 $("#loginButton).attr("href", document.location.hostname + "/login") 这样的东西不起作用,因为它只是指向 baseurl/baseurl/login 这也是无效的

如果登录页面的 URL 始终是https://yelpcamp-asool.c9users.io/login,那么下面的代码将正常工作。

<a id="loginButton" href="/login">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM