2

我试图让这段代码在drupal上工作,但只有当我按f5时才有效,链接或表单不起作用,知道这段代码有什么问题吗?

我的目标是在您关闭选项卡或关闭资源管理器时终止会话。此代码是对此链接的修改: http: //eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/#comment-6936

<code>
(function($) {


var validNavigation = false;

  var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
  var leave_message = 'Do you want to exit?';

  function goodbye(e) {
    if (!validNavigation) {
      if (dont_confirm_leave!==1) {
        if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = leave_message;
        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
        //return works for Chrome and Safari
        // window.open('DelLogged.php?Id=');
        return leave_message;
      }
    }
  }

   // Attach the event click for all links in the page
  $("a").click(function () {
    validNavigation = true;
    alert("Link press");
  });

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

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

window.onbeforeunload=goodbye;

})(jQuery);
</code>
4

1 回答 1

1

我认为您没有(function($){}(jQuery));正确使用闭包,因为括号的书写方式不正确。你写过

 (function($) {
   ...
 })(jQuery);

关闭初始括号之前(jQuery);

尝试更改此设置,看看是否可以解决问题:)

于 2013-02-22T10:29:42.023 回答