2016-12-08 18 views
0

我對Web開發相當陌生,而且我正在努力使用JavaScript獲取登錄表單。基本上,我試圖將它裝配到Firebase上,但在此之前;每次點擊登錄按鈕時,它都會在瀏覽器URL中以文本形式發送電子郵件和密碼,並且不會重定向,無論我將其重定向到哪裏。爲什麼HTML按鈕不響應JavaScript重定向?

我試圖重定向到另一個HTML,我已經在同一個目錄中,我試過Google的主頁。無論我用JavaScript做什麼,什麼都不工作。

這是我的HTML

注意:未添加CSS,所以它不會讓外表和感覺。

<div class="login-form"> 
           <div class="form-help-text">Looking for the Advertiser Login? <a href="advertiser-Login.html">Click here</a></div> 
           <div class="login-form-container"> 
            <form name="loginForm" class="form-horizontal" role="form" novalidate> 
             <div class="form-group" show-errors> 
              <label class="col-sm-4 control-label">Email Address:</label> 
              <div class="col-sm-8"> 
               <div class="input-group"> 
                <input type="email" class="form-control" name="email" id="email" ng-model="formData.username" required autofocus> 
                <span class="input-group-addon"><i class="fa fa-user"></i></span> 
               </div> 
              </div> 
             </div> 
             <div class="form-group" show-errors> 
              <label class="col-sm-4 control-label">Password:</label> 
              <div class="col-sm-8"> 
               <div class="input-group"> 
                <input type="password" class="form-control" name="password" id="password" ng-model="formData.password" required> 
                <span class="input-group-addon"><i class="fa fa-lock"></i></span>             
               </div> 
              </div> 
             </div> 
             <div> 
              <button id = "loginEmail" class = "btn btn-lg btn-block btn-wrapify-login">Sign In Email</button> 
              <!--<button id = "login" class="btn btn-lg btn-wrapify-login btn-block">Sign In </button>--> 
              <div class="member-login-container"> 
               Not a member yet? <a href="#">Sign Up!</a> 
              </div> 
              <div class="divider"><img src="img/button-divider.png" class="img-responsive"></div> 
              <button class="btn btn-lg btn-fb-login btn-block" <i class="fa fa-facebook-official fa-lg"></i>&nbsp;&nbsp;Sign In With Facebook</button> 
              <div class="divider"></div> 
              <div id="google-signin-button"></div> 
              <p class="text-center m-t-sm"><a href"#"">Forgot Your Password?</a></p> 
             </div> 
            </form> 
           </div> 
          </div> 

這是它的JavaScript。似乎一切都在Chrome JS控制檯檢查時正常工作,但我從來沒有重定向到下一個頁面

const textEmail = document.getElementById("email"); 
const textPword = document.getElementById("password"); 
const btn = document.getElementById("loginEmail"); 
// const email = document.getElementById("signup"); 

btn.addEventListener('click', function(){ 
    // Get email and passwrd 
    const email = textEmail.value; 
    const pword = textPword.value; 
    const auth = firebase.auth(); 

    const promise = auth.signInWithEmailAndPassword(email, pword); 
    console.log("logging in "); 
    promise.catch(function(error){ 
     console.log(error.message); 
    }); 
    // console.log(promise); 
    window.location = "post-login.html"; 
    //window.location.href = "post-login.html"; //just to see if it makes a difference 
    //window.location = "http://www.google.com";//tried this as well 
    //window.location.href = "http://www.google.com";//tried this as well 

}); 

我在做什麼錯?提前致謝。

+0

嘗試加入'方法= POST'到你的''

' – Lal

+0

promise.then(功能(錯誤) { window.location.href =「http://www.google.com」; });',這可能會取消身份驗證,也就是說您不會再進行身份驗證 –

回答

0

你可以嘗試這樣的:<input type="button" onclick="location.href='http://google.com';" value="Redirect" />

0

你的JS點擊提交按鈕時運行。

您將location設置爲一個新值,然後JS完成並且正常表單提交接管並提交表單。

捕獲事件對象,並防止按鈕的默認行爲時,JS是成功的:

btn.addEventListener('click', function(ev){ 
    ... 
    ev.preventDefault(); 
});