我正在使用下面的代码来检测浏览器关闭事件,然后使会话无效。如果我直接点击浏览器关闭按钮,它工作正常。如果我单击页面中的某些 <a href > 则 window.onbeforeunload 将被触发并且会话无效。
如果有人知道这个问题的解决方案,请告诉我。
/*JS code begins here*/
/*Global js variable to decide whether to call session invalidate function*/
    var validNavigation = false;
        //Called when page loads initially
        jQuery(document).ready(function() {
           //Call to wireupEvents
        wireUpEvents();
                });
        //Function called when the page loads
            function wireUpEvents() {
            window.onbeforeunload= function() {
                if (!validNavigation) {
                           /*This JS function calls my managed bean method to invalidate session.*/
                    windowCloseJsFunction();
                }
            }
        // Attach the event keypress to exclude the F5 refresh
            jQuery(document).bind('keypress', function(e) {
                if (e.keyCode == 116) {
                    alert('116');
                    validNavigation = true;
                }
            });
            // Attach the event click for all links in the page
            jQuery("a").bind("click", function() {
                alert('click a');
                validNavigation = true;
            });
            // Attach the event submit for all forms in the page
            jQuery("form").bind("submit", function() {
                alert('form');
                validNavigation = true;
            });
            // Attach the event click for all inputs in the page
            jQuery("input[type=submit]").bind("click", function() {
                alert('input');
                validNavigation = true;
            });
        //Attach button click for all inputs in the page
            jQuery("input[type=button]").bind("click", function() {
          validNavigation = true;
        });
        }
    /*JS code ends here*/