页面出口上的弹出框 [英] Popup box on page exit

查看:79
本文介绍了页面出口上的弹出框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是每当页面退出或离开页面时弹出一个弹出框.现在我有

What I am trying to do is make a pop-up box any time a page exits or is navigated away from. Right now I have

<script type="text/javascript">
function box()
{
    var r=confirm("Message");
    if (r==true)
    {
        window.location.href="yes.html";
    }
    else
    {
        window.location.href="no.html";
    }
}
</script>


<body onunload="box();">

我对此有2个问题:

  1. 仅当您实际离开页面,刷新,新的url等内容时,它才会显示该框.如果退出选项卡或浏览器,则不会弹出该框.

  1. It only shows the box if you actually navigate away from the page, refresh, new url etc. If you exit the tab or browser, the box doesnt pop up.

无论您按什么按钮,它只会将您发送到您最初尝试的位置,而不会将您发送到no.htmlyes.html.

No matter what button you press, it just sends you where you tried to go originally, it never sends you to no.html or yes.html.

有人可以告诉我这怎么可能吗?

Could someone tell me how this is possible?

推荐答案

尝试一下:

<script type="text/javascript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
      setTimeout(function() {
        setTimeout(function() {
           window.location.href="yes.html";
        }, 1000);
    },1);
    return "Message";
  }
</script>

您只能捕获停留在页面上的选项,不能覆盖离开页面的用户.类似于:如何知道用户是否在卸载对话框上单击Java上的取消"?

You can only catch the stay on the page option, you cannot override a user leaving the page. similar to: Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

这篇关于页面出口上的弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆