简体   繁体   English

如何使用JavaScript重定向到首页

[英]How redirect to homepage using JavaScript

I am using MVC. 我正在使用MVC。 I am showing Thank you page(view) after user reset his password. 用户重置密码后,我正在显示“谢谢”页面(视图)。 I like to redirect to Home page by clicking anywhere in page,( I am not allow to use jquery only JavaScript) This is a method that return Thank you page view: 我想通过单击页面中的任意位置来重定向到主页,(我不允许仅使用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");
    }

and this is ThankYouResetPassword View that is JavaScript Modal: 这是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>

When the box displays you could add an event listener to the body of the page. 当该框显示时,您可以将事件侦听器添加到页面主体。

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

We want to use location.replace because we don't want them to hit refresh or back and resubmit the form. 我们要使用location.replace,因为我们不希望他们点击刷新或返回并重新提交表单。 This is less of an issue with modern browsers, but it's still worth protecting against. 对于现代浏览器来说,这不是什么大问题,但仍然值得防范。

You can do the following 您可以执行以下操作

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

Why not add a onclick attribute ? 为什么不添加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