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 に答える