如何在不使用onbeforeunload或卸载

时间:2017-11-15 08:45:39

标签: javascript jquery asp.net browser

我想在标签或浏览器关闭时调用我的功能。 我尝试了window.onbeforeunload或window.unload方法,但是这个方法在页面重新加载或刷新时调用我的函数。 我的代码是:

 $(document).ready(function () {
            IsClose();
        });

        var validNavigation = false;

        function IsClose() {

            debugger;
            // Attach the event click for all links in the page
            $(document).on("click", "a", function () {
                validNavigation = true;
            });
            // Attach the event submit for all forms in the page
            $(document).on("submit", "form", function () {
                validNavigation = true;
            });
            // Attach the event click for all inputs in the page
            $(document).bind("click", "input[type=submit]", function () {
                validNavigation = true;
            });
            $(document).bind("click", "button[type=submit]", function () {
                validNavigation = true;
            });
            // Attach the event keypress to exclude the F5 refresh
            $(document).bind('keypress', function (e) {
                debugger;
                if (e.keyCode == 116) {
                    validNavigation = true;
                }
            });
            $(window).unload(function () {
                debugger;
                if(!validNavigation)
                    goodbye();
            });

            var dont_confirm_leave = 0;

            function goodbye(e) {
                debugger;
                if (!validNavigation) {
                    debugger;
                    if (dont_confirm_leave !== 1) {
                        DeleteLoggedinUser();
                    }
                }
            }     

            return false;
        }

    function DeleteLoggedinUser(){
        // My Code here than i want to do when tab or browser closed
    }

任何人都可以建议我如何使用java脚本或JQuery检测浏览器关闭或关闭标签

0 个答案:

没有答案