2017-10-11 1 views
0

자바 스크립트로 리디렉션하면 사용자를 다른 웹 사이트로 리디렉션하려고합니다. 아래의 코드는 작동하지 않으며 그 이유를 알 수 없습니다.sucefull 로그인시

나는 사용했다; 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> 

를 호출하는 콜백을 사용

;(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

을 점검 할 때 호출 JS . 위치

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