2015-09-23 234 views
0

我試圖在登錄嘗試後重定向頁面,但由於某種原因它拒絕這麼做。我認爲這可能是btoa功能,但我不確定。代碼的alert部分起作用,但不是重定向部分。Javascript頁面不重定向

不管怎麼說,這裏是我的代碼

function setCookie(cname, cvalue, cname2, cvalue2, exdays) { 
    var d = new Date(); 

    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); 

    var expires = "expires=" + d.toUTCString(); 

    document.cookie = cname + "=" + cvalue + "; " + expires; 
    document.cookie = cname2 + "=" + cvalue2 + "; " + expires; 
} 

$(document).ready(function(e) { 
    $('#username').focus(); 

    // upon login, set the cookie 
    $('#submit').click(function() { 
     if ($('#username').val() != "" && $('#password').val() != "") { 

      /* 
       -------- Base64 Encryption logic -------- 
       This is in no way meant to be a secure encryption method, 
       but it is extremely useful for writing obfuscated strings to either a document 
       or a cookie file without needing to worry about quotes or characters breaking things. 
      */ 

      // encrypt the password with base 64 (using the btoa function) 
      var encrypted_password = btoa($('#password').val()); 

      // set cookie 
      setCookie("username", $('#username').val(), "password", encrypted_password, 365); 

      // to decrypt, use atob function 
      // var decrypt = atob(encrypted_password); 

      // test to make sure it worked 
      // alert(encrypted_password); 
      // alert(decrypt); 
      window.location = "test.html"; 
     } else { 
      alert("All fields must be filled out"); 
     } 
    }); 
}); 

HTML:

<form> 
       <div class="form-group"> 
        <label for="username">Username</label> 
        <input type="text" class="form-control" name="username" id="username" placeholder="Enter username here"> 
       </div> 

       <div class="form-group"> 
        <label for="password">Password</label> 
        <input type="text" class="form-control" name="password" id="password" placeholder="Enter password here"> 
       </div> 

       <div class="form-group"> 
        <button type="submit" class="btn btn-default" id="submit">Login</button> 
       </div> 
      </form> 
+0

你試過window.location.href =「test.html的」 – Valeklosse

+0

這應該工作,如果有一個名爲'test.html'在同一個目錄下的文件,請檢查您的控制檯有任何錯誤。 –

+0

試試'window.location.href' - 這是否工作? @SpencerWieczorek它甚至應該工作,如果沒有 - 這將只是一個'文件未找到'又名'404'。 – somethinghere

回答

3

您應該替換您的#submit單擊事件與您取消的form提交事件。當防止對這種活動的瀏覽器不會postget像一個普通的頁面中的數據並不會刷新頁面,它會要求你做就是它,這意味着你可以設置你的前鋒之後。因此,改變從這個包裝的jQuery:

$('#submit').click(function(){ 
    ... code ... 
}); 

這樣:

$('#myForm').on('submit', function(event){ 
    event.preventDefault(); 
    ... code ... 
}); 

我就會把這個片斷,但如果你嘗試提交表單SO拋出異常。

-3

我希望這將幫助你.. 只是把它保存爲一個單獨的HTML頁面,但一定要配置腳本中的兩個變量..

form name="redirect"> 
<center> 
<font face="Arial"><b>You will be redirected to the script in<br><br> 
<form> 
<input type="text" size="3" name="redirect2"> 
</form> 
seconds</b></font> 
</center> 

<script> 
<!-- 

//change below target URL to your own 
var targetURL="http://***********.com" 
//change the second to start counting down from 
var countdownfrom=10 
var currentsecond=document.redirect.redirect2.value=countdownfrom+1 
function countredirect(){ 
if (currentsecond!=1){ 
currentsecond-=1 
document.redirect.redirect2.value=currentsecond 
} 
else{ 
window.location=targetURL 
return 
} 
setTimeout("countredirect()",1000) 
} 
countredirect() 
//--> 
</script> 
+1

這看起來像各種錯誤,並沒有解決問題。問題不是關於創建倒計時,而是關於表單提交工作不正確。 – somethinghere