如何获取if / else语句的当前url

时间:2018-06-06 19:44:14

标签: javascript html css

我正在尝试编写一个if / else语句,在最后检查当前页面网址的“#log_in”。

在我的页面上,当您点击登录或在顶部注册时,它会分别向您发送一个新页面,其中包含网址“./log_in.html#log_in”或“./log_in.html#sign_up”。我希望我的注册框显示网址中有“#sign_up”,当然登录时也是如此。

这是我到目前为止所得到的:

if (??? === "./log_in.html#log_in"){
loginButton.style.backgroundColor = "#a8661f";
signupPage.style.visibility = "hidden";
loginPage.style.visibility = "visible";
signupButton.style.backgroundColor = "#d18029";
title.innerHTML = "CussFeed | Log In";
} else {
signupButton.style.backgroundColor = "#a8661f";
loginButton.style.backgroundColor = "#d18029";
loginPage.style.visibility = "hidden";
signupPage.style.visibility = "visible";
title.innerHTML = "CussFeed | Sign Up";
}

2 个答案:

答案 0 :(得分:3)

window.location.hash会返回您要查找的值

答案 1 :(得分:0)

if (window.location.href === "./log_in.html#log_in") {
    loginButton.style.backgroundColor = "#a8661f";
    signupPage.style.visibility = "hidden";
    loginPage.style.visibility = "visible";
    signupButton.style.backgroundColor = "#d18029";
    title.innerHTML = "CussFeed | Log In";
} else {
    signupButton.style.backgroundColor = "#a8661f";
    loginButton.style.backgroundColor = "#d18029";
    loginPage.style.visibility = "hidden";
    signupPage.style.visibility = "visible";
    title.innerHTML = "CussFeed | Sign Up";
}

使用window.location.href获取页面的当前位置

为什么你被投入。最初?

我认为if条件应该是这样的

if(window.location.href === "/log_in.html#log_in")