延迟重定向(Javascript)

时间:2014-04-29 13:49:00

标签: javascript login delay settimeout window.location

我的JS不会合作和重定向......谁能看到我的问题?整天都在尝试,它没什么用的:(

function validate(form) {
    if (form.username.value==="admin") { 
        if (form.password.value==="1") {
            $('#loginLock').css({opacity: 0});
            $('#loginUnlock').css({opacity: 1});
            document.cookie="username=Admin";
            setTimeout(function(){ window.location.replace = 'vault.php';}, 1500);
        } else {
            alert("Forkert brugernavn eller password");
        }
    } else {
        alert("Forkert brugernavn eller password");
    }
}

3 个答案:

答案 0 :(得分:3)

您可以使用window.location.href

 setTimeout(function(){ window.location.href= 'vault.php';}, 1500);

而不是

setTimeout(function(){ window.location.replace = 'vault.php';}, 1500);

答案 1 :(得分:2)

使用javascript的setTimeout方法:

// redirect to google after 5 seconds
window.setTimeout(function() {
    window.location.href = 'http://www.google.com';
}, 5000);

答案 2 :(得分:1)

为什么使用window.location.replace?也许是问题所在!

**assign(url):** Load the document at the provided URL.
**replace(url):** Replace the current document with the one at the provided URL. The difference from the  assign() method is that after using  replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

尝试仅使用windows.location.url = "";关于usin jQuery,只需使用setTimeOut JS方法。如果没有必要,请不要混用jQueryJavascript