2012-10-29 25 views
2

我想讓這個代碼工作在drupal,但只適用於當我按f5,鏈接或窗體不工作,任何想法這個代碼有什麼問題?添加javascript到drupal是不承認一些jquery

我的目標是關閉選項卡或關閉資源管理器時關閉會話。此代碼是從這個鏈接的修改: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> 
+0

你看到頁面上的任何JavaScript錯誤?如果是這樣,那可以殺死頁面上的任何進一步的js。 –

+0

我看不到任何錯誤,如果我按f5腳本工作完美,但我無法工作其他檢測,如「一個」或「形式」 –

回答

1

我覺得你沒有正確使用(function($){}(jQuery));關閉,因爲支架不寫入正確的方式。你寫

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

關閉初始括號前(jQuery);

嘗試改變這一點,讓我們看看是否可以解決問題:)