簡體   English   中英

JavaScript頁面未重定向

[英]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提交事件。 當阻止此類事件時,瀏覽器將不會像常規頁面那樣postget數據,也不會刷新頁面,這將要求為此做,這意味着您可以在以后進行設置。 因此,從此更改包裝的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM