【问题标题】:Adding javascript to drupal is not recognize some jquery将 javascript 添加到 drupal 无法识别某些 jquery
【发布时间】:2012-10-29 13:28:33
【问题描述】:

我正在尝试让这段代码在 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>

【问题讨论】:

  • 您是否在页面上看到任何 javascript 错误?如果是这样,那可能会杀死页面上的任何其他 js。
  • 我看不到任何错误,如果我按 f5,脚本可以完美运行,但我无法进行其他检测,例如“a”或“form”

标签: javascript session drupal


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 2020-08-08
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2014-06-08
    相关资源
    最近更新 更多