单击浏览器后退按钮时如何警告用户 [英] How to warn the user when click on the browser back button

查看:31
本文介绍了单击浏览器后退按钮时如何警告用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户单击浏览器后退按钮"时发出警告,然后在确认后重定向.以下是我的 JS 代码,但仅适用于 Firefox,我想让它也适用于 chrome 和其他浏览器.

I would like to warn the user when they click on the browser "back button" then redirect them upon confirmation. Below is my JS code but works only in firefox, I would like to make it work in chrome and other browsers as well.

注意:为了在 chrome 中触发事件,我需要先点击页面正文,然后点击浏览器的返回"按钮(这不好).

Note: In order for the event to be fired in chrome, I need first to click on the page body then click on browser "back" button(this is not good).

请帮忙.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page2</title>
</head>
<body>
  <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>
  <script>
    $(function(){
      window.history.pushState({page: 1}, "", "");
      window.onpopstate = function(event) {
        if(event){
          var confirm = window.confirm("Please, note that you may lose your move details by returning to the previous page.");
        }
      }
    });
  </script>
</body>
</html>

推荐答案

我找到了一个有用的链接,它解释了 Chrome 和其他浏览器的这种行为.

I'd found a useful link, which explains such a behavior of Chrome and other browsers.

"Chrome 计划让你远离那些乱七八糟的后退按钮的网站"

上述帖子中提到的一些令人讨厌的网站,使用这种行为,对他们有利.因此,似乎像 Chrome 这样的浏览器有意地通过后退按钮"阻止了这些偷偷摸摸的黑客行为(显然是由于安全原因和流畅的用户体验).此外,我怀疑是否有人发现了一个很好的黑客,那么 Chrome 开发人员将再次改进他们的安全检查,如帖子中所述.

Some of the nuisance sites mentioned in above post, uses this behavior, in their favor. So, it seems that browsers like Chrome are intentionally preventing these kind of sneaky hacks with the "back button" (obviously due to security reasons and smooth user-experience). Also, I doubt if anyone finds a pretty hack, then Chrome developers will again evolve their security checks, as said in the post.

无论如何,我们仍然拥有 Firefox .祝你好运;)

Anyways, we still have Firefox with us. Good luck ;)

这篇关于单击浏览器后退按钮时如何警告用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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