【问题标题】:Redirect to homepage after alert message is closed?警报消息关闭后重定向到主页?
【发布时间】:2017-05-10 12:05:17
【问题描述】:

我有一个 javascript 警报作为按钮单击事件的一部分,我想在单击警报后重定向到主页。

这可能吗?

这是我的提醒

string script = @
    "<script type='text/javascript'>
        alert('Booking Complete! Thank you for choosing Euro-City-Tours');
    </script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);

【问题讨论】:

标签: javascript c# asp.net


【解决方案1】:

要重定向到另一个页面,您可以使用:

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

谢谢,

【讨论】:

    【解决方案2】:

    你应该这样做,因为警报是一个对话框,并在它关闭后执行代码。

    string script = "<script type=\"text/javascript\">
    
    alert('Booking Complete! Thank you for choosing Euro-City-Tours');
    window.location.replace('my home page url');
    
    </script>";
    

    【讨论】:

      【解决方案3】:

      只是为了让它有点花哨,并让用户有机会在重定向之前阅读并关闭警报。

       var delay = 5000;
       setTimeout(function () {
                  window.location.href = "/Home/Index";
                   }, delay); 
       alert("You will be redirected to Home Page after 5 seconds");
      

      警报无论如何都会执行。

      【讨论】:

      • 我的意思是您需要延迟,以便用户在重定向到您的主页之前有机会阅读并关闭警报。
      【解决方案4】:

      我改变了身份。在我看来,这读起来更好:

      string script = @"<script type='text/javascript'>
                      alert('Booking Complete! Thank you for choosing Euro-City-Tours');
                      window.location.href = ""http://stackoverflow.com"";
                      </script>";
      
      ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
      

      【讨论】:

        【解决方案5】:

        alert()之后添加重定向,脚本将停止,直到警报被点击为止:

        alert('Hello!');
        window.location = 'somewhere.html';
        

        【讨论】:

          【解决方案6】:

          您可以尝试confirm 功能,前提是它是一个警告框。 这里发生的是window.location 会将您的当前页面重定向到指定的网址

          var clickedBtn=confirm("Alert box");
          if (clickedBtn==true) {
            //pressed ok. Redirect to url
            window.location = "http://www.yoururl.com";
          } else {
            //pressed cancel
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-11
            相关资源
            最近更新 更多