window.location.replace无效

时间:2013-08-19 06:44:51

标签: javascript window.location

我写了一个javascript函数,发布一个表单并重定向到主页。我使用window.location.replace来到主页。但是不是替换url,而是将函数添加到当前url前面的url。问题是什么?

    $('#submit_fourth').click(function () {

    // send data to server to save in table dbo.aspnet_Users  =================================================
    var firstname = $("#firstname").val();
    var lastname = $("#lastname").val();
    var username = $("#username").val();
    var password = $("#password").val();
    var email = $("#email").val();
    $.post("/Account/Register", { FirstName: firstname, LastName: lastname, UserName: username, Password: password, ConfirmPassword: password, Email: email });
    //send information to server
    alert('Congratulations! Your account has been sucessfully created.');
    //get back to the login screen
    window.location.replace("dgsmart/Central/Login");

当前网址是184.180.25.240/dgsmart/account/register注册按钮点击后变为184.180.25.240/dgsmart/account/central/login 我想要url lyk这个184.180.25.240/dgsmart/central/login

2 个答案:

答案 0 :(得分:0)

我假设您正在使用表单onsubmit事件来调用某个函数,如果是这样,您必须通过返回false来阻止默认行为; (例如)

window.location.replace("absolute-address"); /*window.location.replace('http://example.com/#' + initialPage); */
return false; //prevent the default behavior of the submit event

答案 1 :(得分:-1)

您还可以使用此代码设置理想的网址...

<script>
    location.href='RegisterNew.php';
</script>