2017-10-11 16 views
0

をJava Scriptでリダイレクトすると、ユーザーを別のWebサイトにリダイレクトしようとしています。以下のコードは動作しませんし、理由を説明することはできません。sucefullログイン時に

私はこれを使用しました。 window.location.href、document.location.href、window.location.replaceもあります。あなたのボタンがフォームをsumbitしようとしている

<!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

をチェックして行わときに呼び出すコールするコールバックを使用場所

window.location = "http://www.stackoverflow.com"; 
関連する問題