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