Javascript页面未重定向

时间:2015-09-23 16:03:58

标签: 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 = "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>

2 个答案:

答案 0 :(得分:3)

您应该将#submit点击事件替换为您取消的form提交事件。当阻止此类事件时,浏览器不会像常规页面那样postget数据并且无法刷新页面,那么它将需要为此做到这一点,这意味着你可以事后设定你的前锋。所以改变包装jquery:

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

到此:

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

我会把它放在一个片段中,但如果你尝试提交表单,那么SO会抛出异常。

答案 1 :(得分:-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>