简体   繁体   English

当我刷新页面时 JavaScript 停止工作

[英]JavaScript stops working when I refresh page

I have this block of JavaScript code:我有这段 JavaScript 代码:

var input = document.getElementById("password_field");
input.addEventListener("keyup", function(event) {
  if (event.keyCode === 13) {
   event.preventDefault();
   document.getElementById("login").click();
  }
});

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        document.getElementById("user_div").style.display = "block";
        document.getElementById("login_div").style.display = "none";

        var user = firebase.auth().currentUser;

        if(user != null) {
            var email_id = user.email;
            var email_verified = user.emailVerified;
            document.getElementById('user_para').innerHTML = "You are logged in as<strong> " + email_id + "</strong>.";
            if (email_verified === false) {
                document.getElementById('user_verified').innerHTML = "<strong>You are not verified. Please check for an email from perason for a verification link. You will not be able to access anything without verification.";
            } else {
                document.getElementById('user_verified').innerHTML = "";
                document.getElementById('sandboxaccess').style.display = "block";
            }
        }
      
      
    } else {
        document.getElementById("user_div").style.display = "none";
        document.getElementById("login_div").style.display = "block";
    }
  });

function login () {
    var userEmail = document.getElementById('email_field').value;
    var userPass = document.getElementById('password_field').value;

    firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        window.alert("Error: " + errorMessage);
      });


    
}

function logout() {
    firebase.auth().signOut();
}

It works fine initially, but when I refresh the page, all of the JavaScript stops working.它最初工作正常,但是当我刷新页面时,所有 JavaScript 都停止工作。 It works perfectly fine on Apache localhost (refreshing works on localhost too).它在 Apache localhost 上运行得非常好(刷新也可以在 localhost 上运行)。 What is the solution to this?解决这个问题的方法是什么?

Here it is before refresh:这是刷新前:

before refresh刷新前

Here it is after refresh:这是刷新后的样子:

after refresh刷新后

The logout button is in both.注销按钮在两者中。

Errors on console控制台上的错误

I think the code is executed before the DOM is loaded.我认为代码是在加载 DOM 之前执行的。
how about this:这个怎么样:

document.addEventListener('DOMContentLoaded', function (){
  // your code
});

Remove this line of code from your program:从你的程序中删除这行代码:

var user = firebase.auth().currentUser

You already have a user object as the parameter of your callback function.您已经有一个user对象作为回调函数的参数。 The currentUser returned here might actually be wrongly null and overwriting the correct one being passed in.此处返回的 currentUser 实际上可能错误地为 null 并覆盖了传入的正确用户。

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

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