繁体   English   中英

如何使用JavaScript重定向到首页

[英]How redirect to homepage using JavaScript

我正在使用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视图,它是JavaScript模式:

<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>

当该框显示时,您可以将事件侦听器添加到页面主体。

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

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

您可以执行以下操作

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

为什么不添加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>

暂无
暂无

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

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