简体   繁体   English

window.location.replace无法正常工作

[英]window.location.replace not working

i have written a javascript function thats posts a form and redirect to home page . 我已经写了一个javascript函数,可以发布表格并重定向到主页。 Im using window.location.replace to get to home page. 我正在使用window.location.replace转到主页。 but instead of replacing the url the function is appending the url in front of current url. 但是该函数不是替换url,而是将url附加在当前url的前面。 Whats the problem? 有什么问题?

    $('#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");

current url is 184.180.25.240/dgsmart/account/register after register button click it becomes 184.180.25.240/dgsmart/account/central/login i want url lyk this 184.180.25.240/dgsmart/central/login 单击注册按钮后,当前网址为184.180.25.240/dgsmart/account/register

I assume you are using a form onsubmit event to call some function, if so you have to prevent the default behavior by return false; 我假设您正在使用表单onsubmit事件来调用某些函数,如果这样做,则必须通过返回false来防止默认行为; (for example) (例如)

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

You can also using this code to set your ideal url... 您还可以使用此代码设置理想的网址...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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