0

I want to call my function when tab or browser closing. I have try window.onbeforeunload or window.unload method but this method call my function when page reload or refreshing. my code is:

 $(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
    }

Can anyone suggest me how can i detect browser closing or tab closing using java script or JQuery

Parita Navadiya
  • 83
  • 2
  • 12

0 Answers0