2017-10-11 56 views
0

成功登錄,我試圖重新指引用戶到另一個網站。下面的代碼,不起作用,不能爲什麼。重新定向在javaScript

我已經使用過; window.location.href,document.location.href和window.location.replace。

這裏是我的代碼:

<!DOCTYPE html> 
<html> 

<head> 

<title></title> 


<link rel="stylesheet" href="login.css" type="text/css"> 
<script src="login.js"> 



    </script> 

</head> 

<body> 
<div class="login-page"> 
    <div class="form"> 
    <form class="register-form"> 
     <input type="text" placeholder="name"/> 
     <input type="password" placeholder="password"/> 
     <input type="text" placeholder="email address"/> 
     <button>create</button> 
     <p class="message">Already registered? <a href="#">Sign In</a></p> 
    </form> 
    <form class="login-form"> 
     <input type="text" placeholder="username" id="username" value="" required/> 
     <input type="password" placeholder="password" id="password" value="" required/> 
    <button onclick="myFunction()">Login</button> 
     <p class="message">Not registered? <a href="#">Create an account</a></p> 


    <script> 

    function myFunction(){ 

     var uName= document.getElementById("username").value; 
     //alert(uName); 
     var pwd= document.getElementById("password").value; 
     //alert(pwd); 

     var result = loginCheck(uName, pwd);//loginCheck function external 

     //if true    
     if(result==true){ 

      //re-direct to stack overflow 
      window.location.href= "http://stackoverflow.com"; 

     } 

     else{ 

      alert("you have failed"); 
     } 

    } 



    </script> 
+0

單擊按鈕時會發生什麼情況?控制檯上顯示的是什麼?有什麼錯誤? – Alfabravo

+1

'result'的值是多少?在'if'語句之上做一個'console.log(result)' –

+0

這個問題在函數內部,你沒有發佈。上面的代碼完美地工作,'loginCheck()'是問題 –

回答

2

你的按鈕嘗試透過一種形式。這可能是一個奇怪的解決方案,但更改按鈕type屬性爲提交

例如:

<button type="button" onclick="myFunction()">Login</button> 
0

首先,你應該做的jQuery的的document.ready或匿名函數整個動作。其次,在loginCheck定義,使用回調調用結果檢查

<button id="buttonId">Login</button> 

然後在JS

;(function(){ 
document.getElementById('buttonId').addEventListener('click',function(){ 
    var uName= document.getElementById("username").value; 
    //alert(uName); 
    var pwd= document.getElementById("password").value; 
    //alert(pwd); 

    loginCheck(uName, pwd, function(){ 
    //if true    
    if(result==true){ 
     //re-direct to stack overflow 
     window.location.href= "http://stackoverflow.com"; 
    } 
    else{ 
     alert("you have failed"); 
    } 
    }); 
}); 
})(); 

在功能參數添加回調,做檢查

0

重定向使用窗口時調用.location

window.location = "http://www.stackoverflow.com";