简体   繁体   English

JavaScript页面未重定向

[英]Javascript page not redirecting

I'm trying to make a page redirect after a login attempt, but for some reason it refuses to do so. 我试图在登录尝试后进行页面重定向,但是由于某种原因,它拒绝这样做。 I think it may be the btoa function but I am not sure. 我认为这可能是btoa功能,但我不确定。 The alert section of code works, but not the redirect part. 代码的Alert部分起作用,但重定向部分无效。

Anyways, here is my code 反正这是我的代码

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: 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>

You should replace your #submit click event with a form submission event that you cancel. 您应该将您的#submit click事件替换为您取消的form提交事件。 When preventing that kind of event the browser won't post or get the data like a regular page and won't refresh the page, it will require you to do that for it, which means you can set your forward afterwards. 当阻止此类事件时,浏览器将不会像常规页面那样postget数据,也不会刷新页面,这将要求为此做,这意味着您可以在以后进行设置。 So change the wrapping jquery from this: 因此,从此更改包装的jquery:

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

to this: 对此:

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

I would have put this in a snippet but SO throws exceptions if you try form submissions. 我会将其放在摘要中,但是如果您尝试提交表单,则SO会引发异常。

i hope this will help you out.. just save it as a seperate HTML page, but make sure to configure the two variables inside the script.. 我希望这对您有帮助。.只需将其另存为单独的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM