为什么我在删除 window.location("url") 后仍会被重定向
Why do I get redirected even after I deleted window.location("url")
我创建了一个登录表单并将提交按钮设置为使用 window.location.replace("home.html")
重定向到 home.html,我还尝试了 window.location.href = "home.html"
<button onclick="signIn()">Sign In</button>
functon signIn(){
//Username input
var email = document.getElementById("email");
//Password input
var password = document.getElementById("password");
//Firebase function for signing in
auth.signInWithEmailAndPassword(email.value, password.value);
}
//If user is signed in
auth.onAuthStateChanged(functon(user){
if(user){
window.location.href = "home.html"
}
else{
alert("No credentials")
}
})
但是,我删除了重定向命令和home.html
。但只要用户登录,它就会重定向到一个不再存在的页面。即使没有 window.location.replace()
<button onclick="signIn()"></button>
function signIn(){
// The username input
const userEmail = document.getElementById("email").value;
//The password input
const userPass = document.getElementById("password").value;
//Firebase function for signing in
auth.signInWithEmailAndPassword(userEmail, userPass).catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error: " + errorMessage);
});
}
结果:
const authObserver = auth.onAuthStateChanged(functon(user){
if(user){
window.location.href = "home.html"
}
else{
alert("No credentials")
}
})
您必须退订 Auth 观察员。你在那里有评论 //If user is signed in
但实际上每次 Auth 状态改变时这个观察者都会运行,即用户登录或注销。
function signIn(){
// unsubscribing from Auth observer
authObserver()
// The username input
const userEmail = document.getElementById("email").value;
}
当调用 signIn
函数时,调用 authObserver()
将取消订阅 Auth 更改,即 onAuthStateChanged
将不再自行重定向用户。
我创建了一个登录表单并将提交按钮设置为使用 window.location.replace("home.html")
重定向到 home.html,我还尝试了 window.location.href = "home.html"
<button onclick="signIn()">Sign In</button>
functon signIn(){
//Username input
var email = document.getElementById("email");
//Password input
var password = document.getElementById("password");
//Firebase function for signing in
auth.signInWithEmailAndPassword(email.value, password.value);
}
//If user is signed in
auth.onAuthStateChanged(functon(user){
if(user){
window.location.href = "home.html"
}
else{
alert("No credentials")
}
})
但是,我删除了重定向命令和home.html
。但只要用户登录,它就会重定向到一个不再存在的页面。即使没有 window.location.replace()
<button onclick="signIn()"></button>
function signIn(){
// The username input
const userEmail = document.getElementById("email").value;
//The password input
const userPass = document.getElementById("password").value;
//Firebase function for signing in
auth.signInWithEmailAndPassword(userEmail, userPass).catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error: " + errorMessage);
});
}
结果:
const authObserver = auth.onAuthStateChanged(functon(user){
if(user){
window.location.href = "home.html"
}
else{
alert("No credentials")
}
})
您必须退订 Auth 观察员。你在那里有评论 //If user is signed in
但实际上每次 Auth 状态改变时这个观察者都会运行,即用户登录或注销。
function signIn(){
// unsubscribing from Auth observer
authObserver()
// The username input
const userEmail = document.getElementById("email").value;
}
当调用 signIn
函数时,调用 authObserver()
将取消订阅 Auth 更改,即 onAuthStateChanged
将不再自行重定向用户。