【问题标题】:On using "confirm('message')" in JavaScript, making the browser navigation buttons disabled在 JavaScript 中使用“confirm('message')”,禁用浏览器导航按钮
【发布时间】:2019-10-12 21:08:18
【问题描述】:

我有一种情况,当用户单击“返回”按钮时,我必须使用 JavaScript 在页面上提示“确认”。结果将决定是否留在页面上。

我用过,如下:

   window.onbeforeunload = function () {

         var answer = return confirm("Please note by clicking the back button will reset your selection")

         if (!answer) {
            event.preventDefault();
         }
         else{

         // do something
         }
 };

现在,即使这会在页面上提示弹出窗口,仍然可以单击后退按钮将用户带到上一页。

另一方面,通过使用以下内容。

   window.onbeforeunload = () => {return '';}

我收到消息“您所做的更改可能无法保存。”,阻止页面上的“返回”、“前进”、“刷新”和所有此类按钮。

我希望在“confirm('message')”上发生同样的事情(阻止这些按钮)。

【问题讨论】:

    标签: javascript jquery html google-chrome browser


    【解决方案1】:
    function Confirm_something() {
        history.pushState(null, null, location.href);
          window.onpopstate = function () {
            history.go(1);
          };
        return confirm("Text you want?");
    }
    
    if(Confirm_something){
       console.log('user say yes'); //then call your save edit or delete functions.
    }
    else{
       console.log('user say no');
    }
    

    【讨论】:

    • 不过,当有确认提示时,我可以点击返回按钮。
    • 我做了更改,现在试试。
    • 不,后退按钮又可以访问了。而且,在我进行必要的更改后,确认提示没有弹出。
    • 确认提示在我进行必要的更改后没有弹出。此外,如果我们连续多次点击它,它会返回。
    • 有什么解决方案可以解决这个问题?
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2011-07-26
    • 2011-08-06
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    相关资源
    最近更新 更多