我試圖在登錄嘗試後重定向頁面,但由於某種原因它拒絕這麼做。我認爲這可能是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>
你試過window.location.href =「test.html的」 – Valeklosse
這應該工作,如果有一個名爲'test.html'在同一個目錄下的文件,請檢查您的控制檯有任何錯誤。 –
試試'window.location.href' - 這是否工作? @SpencerWieczorek它甚至應該工作,如果沒有 - 這將只是一個'文件未找到'又名'404'。 – somethinghere