Javascript页面未重定向
我试图在尝试登录后重定向页面,但由于某些原因,它拒绝这样做。我认为这可能是btoa功能,但我不确定。代码的警报部分有效,但重定向部分无效 不管怎样,这是我的代码Javascript页面未重定向,javascript,jquery,Javascript,Jquery,我试图在尝试登录后重定向页面,但由于某些原因,它拒绝这样做。我认为这可能是btoa功能,但我不确定。代码的警报部分有效,但重定向部分无效 不管怎样,这是我的代码 function setCookie(cname, cvalue, cname2, cvalue2, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "ex
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>
用户名
密码
登录
我希望这能帮到你。。
只需将其另存为单独的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>
formname=“redirect”>
您将被重定向到
秒
您应该将您的#提交
点击事件替换为您取消的表单
提交事件。当防止此类事件发生时,浏览器不会像常规页面那样发布
或获取
数据,也不会刷新页面,这将要求您为此执行此操作,这意味着您可以在以后设置转发。因此,将包装jquery更改为:
$('#submit').click(function(){
... code ...
});
为此:
$('#myForm').on('submit', function(event){
event.preventDefault();
... code ...
});
我会把它放在一个代码段中,但是如果你尝试表单提交,就会抛出异常。你尝试过window.location.href=“test.html”吗如果在同一个目录中有一个名为
test.html
的文件,那么检查你的控制台是否有任何错误。试试window.location.href
-可以吗@SpencerWieczorek如果没有,它甚至应该工作-那只是一个“找不到文件”aka404
@something是的,我知道,我的意思是错误(或它不工作的原因)可能来自其他原因。测试文件在同一个目录中这看起来像是各种错误,无法解决问题。问题不是创建倒计时,而是表单提交工作不正确。