试图让Chrome使用onbeforeunload显示即将离开的页面

问题描述 投票:1回答:1

我正在研究一种Javascript,该Javascript可以对元素执行单击功能,并显示一个弹出窗口,询问您是否真的要离开该网站(关闭标签)。现在,该代码可以在IE和Firefox上正常运行。但是Chrome确实在执行click();方面起着重要作用。它不会显示弹出窗口,询问我是否要离开。我不知道是否需要在Chrome浏览器或其他功能中启用它的功能。这是我正在使用的代码。任何帮助将非常感激。

var validNavigation = false;

function wireUpEvents() {

  var dont_confirm_leave = 0; 
var leave_message = document.getElementById("kioskform:broswerCloseSubmit");
      function goodbye(e) {
        if (!validNavigation) {
          if (dont_confirm_leave!==1) {
            if(!e) e = window.event;
            //for IE
            e.cancelBubble = true;
            e.returnValue = leave_message.click();
            //e.stopPropagation works in Firefox.
            if (e.stopPropagation) {
              e.stopPropagation();
              e.preventDefault();
            }
            //return works for Chrome and Safari
            return leave_message.click();
            alert("Removing information.");
            //add the code to delete the kiosk information here.
            // this is what is to be done.
          }
        }
      }
      window.onbeforeunload=goodbye;

      // Attach the event keypress to exclude the F5 refresh
      jQuery('document').bind('keypress', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
      });

      // Attach the event click for all links in the page
      jQuery("a").bind("click", function() {
        validNavigation = true;
      });

      // Attach the event submit for all forms in the page
     jQuery("form").bind("submit", function() {
        validNavigation = true;
      });

      // Attach the event click for all inputs in the page
     jQuery("input[type=submit]").bind("click", function() {
        validNavigation = true;
      });

    }

    // Wire up the events as soon as the DOM tree is ready
    jQuery(document).ready(function() {
      wireUpEvents();
    });
javascript dom-events onbeforeunload
1个回答
2
投票

您必须在onbeforeunload函数中返回一个字符串以向用户显示消息,另请参见Setting onbeforeunload on body element in Chrome and IE using jQuery

© www.soinside.com 2019 - 2024. All rights reserved.