【问题标题】:How to redirect a page to another page when refresh at second attempt第二次尝试刷新时如何将页面重定向到另一个页面
【发布时间】:2012-01-27 05:12:37
【问题描述】:
<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
   alert("");window.location="index.html";
  }
</script>

我试过这段代码,但它不起作用。

【问题讨论】:

  • 你想做什么?阻止用户离开您的网站?但他们只是第二次尝试离开?
  • @sss 如果我的代码解决了您对答案的探测投票并标记为已接受的答案,以便其他人可以使用此代码。

标签: javascript jquery browser-refresh


【解决方案1】:

在 bbb.jsp 文件中

<script>
    submitFormOkay = false;
    $(document.body).on("click", "a", function() {
        submitFormOkay = true;
    });
    window.onbeforeunload = function(event) {
        event.preventDefault(); 
        if (!submitFormOkay) {
            window.setTimeout(function () { 
                window.location = "index.html";
            }, 0); 
            window.onbeforeunload = null;
        }
    }
</script>

我希望这会有所帮助。干杯!

【讨论】:

    【解决方案2】:

    Windows onload 在所有内容加载后加载,有点慢其他解决方法是使用 document.onload(浏览器兼容性问题)

    window.onload = function () {
      window.location = "/allotment";
    }
    

    【讨论】:

      【解决方案3】:

      这对我有用。不是第二次尝试部分,而是在显示弹出窗口后更改 URL。如果您点击取消,则该功能不会运行。如果单击“重新加载”,则会调用该函数并将您重定向。

      $(window).on('beforeunload', function() {
        $(window).on('unload', function() {
          window.location.href = 'index.html';
        });
      
        return 'Not an empty string';
      });
      

      【讨论】:

        【解决方案4】:

        在bbb.jsp:

        window.onbeforeunload = function() { 
            window.setTimeout(function () { 
                window.location = 'AAA.jsp';
            }, 0); 
            window.onbeforeunload = null; // necessary to prevent infinite loop, that kills your browser 
        }
        

        【讨论】:

        • 完美运行! @Lokesh,您能解释一下这是如何工作的吗?它如何与 setTimeOut 函数一起使用?
        【解决方案5】:

        下面的代码会为你工作

        function confirmExit()
        {
         alert("exiting");
         window.location.href='index.html';
         return true;
        }
        window.onbeforeunload = confirmExit;
        

        【讨论】:

          猜你喜欢
          • 2020-06-15
          • 2018-12-29
          • 1970-01-01
          • 2021-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-02
          • 1970-01-01
          相关资源
          最近更新 更多