简体   繁体   English

在离开/关闭标签之前确认?

[英]Confirmation before leaving/closing of tab?

I'm trying to show a confirmation pop before user close the tab or went to another tab like facebook, gmail, GoDaddy & others do.我试图在用户关闭选项卡或转到另一个选项卡(如 facebook、gmail、GoDaddy 和其他人)之前显示一个确认弹出窗口。

My code working for Firefox but not for other browser like chrome, safari etc.我的代码适用于 Firefox,但不适用于 chrome、safari 等其他浏览器。

<script type="text/javascript">
      var hook = true;
      window.onbeforeunload = function() {
        if (hook) {
          return "Did you save"
        }
      }
      function unhook() {
        hook=false;
      }
    </script>

Call unhook() onClick for button and links在单击按钮和链接时调用 unhook()

<a href="http://example.com" onClick="unhook()">No Block URL</a>

Please help me to get this fixed.请帮我解决这个问题。

If you take a look at the api of window.beforeunload() , you can see that, though widely the basic unload event is supported, a custom message can only be set in internet explorer and certain versions of some browsers.如果您查看window.beforeunload()api ,您可以看到,虽然广泛支持基本卸载事件,但自定义消息只能在 Internet Explorer 和某些浏览器的某些版本中设置。 So just use the normal standard message.所以只需使用普通的标准消息。

This feature (custom messages) was often exploited by malicous sites to interact with user in a harmful or malipulative way.此功能(自定义消息)经常被恶意站点利用,以有害或恶意的方式与用户交互。 This is why many browsers don't support this anymore, until some patch removes the threat for users.这就是为什么许多浏览器不再支持这一点的原因,直到某些补丁消除了对用户的威胁。

Standard message solution:标准消息解决方案:

window.addEventListener('beforeunload', function (e) {
  // Cancel the event
  e.preventDefault();
  // Chrome requires returnValue to be set
  e.returnValue = '';
});

Look at Ouibounce it helps detect when a user is about to leave the page(It looks at the position of the cursor).看看Ouibounce它有助于检测用户何时即将离开页面(它查看光标的位置)。 You could probably build off this library.你可能可以建立这个库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM