如何使用JavaScript重定向到主页

时间:2017-01-18 22:22:25

标签: javascript jquery html

我正在使用MVC。我在用户重置密码后显示感谢页面(查看)。我喜欢通过点击页面中的任意位置重定向到主页,(我不允许仅使用jquery JavaScript)   这是一个返回的方法谢谢页面视图:

    [HttpPost]
    [Route("resetpassword")]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel resetPasswordViewModel)
    {
        string email = resetPasswordViewModel.Email;
        string token = Request["token"].ToString();
        if (ModelState.IsValid)
        {
            if (resetPasswordViewModel.Password == resetPasswordViewModel.ConfirmPassword)
            {
                var user = Task.Run(() => Service.GetUserByEmailAsync(email)).Result;
                if (user != null)
                {
                    userRequest.Id = user.FirstOrDefault().Id;
                    userRequest.Password = resetPasswordViewModel.Password;
                    userRequest.Token = token;
                    await Service.UpdateUserAsync(userRequest);
                }
            }
            else
      //stay in same view and show error if there is any
            {
                ModelState.AddModelError("", "Please enter the same value again.");
                  return View(resetPasswordViewModel);
            }
        }
          else
        {
            ModelState.AddModelError("", "Password is Required Field.");
            return View(resetPasswordViewModel);
        }
        //in case on success toes to ThankYouResetPassword view
        return View("ThankYouResetPassword");
    }

这是ThankYouResetPassword View是JavaScript Modal:

<div id="ThankYou-Page" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <span class="close">x</span>
    <div id="forgot">
        <div class="modal-heading">Forgot Password?</div>
        <hr class="forgot-password-line" />
        <br />
        <p>An email has been sent to you with the password reset link.</p>
        <br />       
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

当显示该框时,您可以向页面正文添加一个事件监听器。

document.body.addEventListener('click', function() {
  document.location.replace('redirectPage.html');
}

我们希望使用 location.replace ,因为我们不希望他们点击刷新或返回并重新提交表单。对于现代浏览器来说,这不是一个问题,但它仍然值得保护。

答案 1 :(得分:0)

您可以执行以下操作

  

window.location =“http://www.yoururl.com/homepage”;

答案 2 :(得分:0)

为什么不添加onclick属性?

<!-- Modal content -->
<div class="modal-content" onclick="window.location.href='/'">
<span class="close">x</span>
<div id="forgot">
    <div class="modal-heading">Forgot Password?</div>
    <hr class="forgot-password-line" />
    <br />
    <p>An email has been sent to you with the password reset link.</p>
    <br />       
</div>
</div>