1

我正在开发一个 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();
    });
4

1 回答 1

2

您必须在 onbeforeunload 函数中返回一个字符串才能向用户显示消息,另请参阅使用 jQuery 在 Chrome 和 IE 中的 body 元素上设置 onbeforeunload

于 2012-09-21T18:25:35.207 回答