javascript - 当用户试图离开当前页面时如何显示弹出窗口?

标签 javascript jquery

我想显示一个确认弹出窗口,当用户试图离开页面(关闭选项卡)时询问他/她“你想离开页面吗?是否”。当用户单击否时,我想将他/她重定向到特定的 URL。 这是我试过的代码:

$(window).on('beforeunload', function(){

   $.Zebra_Dialog('Are you sure to leave the page?', {
      'type':     'question',
       'title':    'Conformation',
       'buttons':  ['Yes', 'No'],
   });
   $("html").on("click",".ZebraDialog_Button_0",function () {
       window.location.href = "http://www.google.com/";
   });
   $("html").on("click",".ZebraDialog_Button_1",function () {
       return true;
   });
   //return false;
});

最佳答案

    window.onbeforeunload = onExit;
    function onExit() {
        return "You have attempted to leave this page. Are you sure?";
    }

试试这个,我已经测试了 firefox 和 chrome,它的工作与 safari 无关。

关于javascript - 当用户试图离开当前页面时如何显示弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40131644/

相关文章:

javascript - 我怎样才能在这里制作一个 html 解决方案而不是使用 javascript hack?

javascript - 添加按钮时 jQuery 幻灯片中断

javascript - 从 json 文件中选择一个特定的数组

javascript - Angularjs 复选框组最小和最大验证限制

javascript - 按钮颜色不变

javascript - 使用 jquery/javascript 将带有下拉列表的 html 表导出到 CSV 文件

javascript - 无法在 Vue props 验证器中使用导入的对象

javascript - Knockout.js 中的级联下拉菜单

javascript - 将ajax请求的内容加载到DOJO中的dom节点

javascript - V8 JavaScript : Why do C++ constructor functions filter Proxy?