[英]Javascript page not redirecting
我试图在登录尝试后进行页面重定向,但是由于某种原因,它拒绝这样做。 我认为这可能是btoa功能,但我不确定。 代码的Alert部分起作用,但重定向部分无效。
反正这是我的代码
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>
您应该将您的#submit
click事件替换为您取消的form
提交事件。 当阻止此类事件时,浏览器将不会像常规页面那样post
或get
数据,也不会刷新页面,这将要求您为此做,这意味着您可以在以后进行设置。 因此,从此更改包装的jquery:
$('#submit').click(function(){
... code ...
});
对此:
$('#myForm').on('submit', function(event){
event.preventDefault();
... code ...
});
我会将其放在摘要中,但是如果您尝试提交表单,则SO会引发异常。
我希望这对您有帮助。.只需将其另存为单独的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.