Rectangle 27 0

javascript Detect Close windows event by Jquery?


By this method, it will be effected while refreshing / navigating the current page.

Even calculating of X Y postion of the mouse event doesn't give you good result.

You can only capture on unload of the current page.

Note
Rectangle 27 0

javascript Detect Close windows event by Jquery?


<script type="text/javascript">
$(document).ready(function() {
  $(document).mousemove(function(e) {
    if(e.pageY <= 5)
    {
        //this condition would occur when the user brings their cursor on address bar 
        //do something here 
    }
  });
});
</script>

Hi there! I edited your post to put all the HTML in code format as it was incorrectly being parsed as HTML tags and not being displayed. In future, pay attention to the preview that shows what your post will look like, as your answer might look significantly different to what you expect!

There is no specific event for capturing browser close event. But we can detect by the browser positions XY.

Note
Rectangle 27 0

javascript Detect Close windows event by Jquery?


$(window).bind("beforeunload", function() { 
    return confirm("Do you really want to close?"); 
})
$(window).unload(function() {
    //do something
}

For the latest jQuery versions recommended use .on() instead of .bind()

The beforeunload event fires whenever the user leaves your page for any reason.

Unload() is deprecated in jQuery version 1.8, so if you use jQuery > 1.8 you can use even beforeunload instead.

best solution its working i have searching and i tried on alot of things but the mos appropriate function which works for me is this one

Note
Rectangle 27 0

javascript Detect Close windows event by Jquery?


$(document).ready(function () {
 var checkCloseX = 0;
        $(document).mousemove(function (e) {
            if (e.pageY <= 5) {
                checkCloseX = 1;
            }
            else { checkCloseX = 0; }
        });

        window.onbeforeunload = function (event) {
            if (event) {
                if (checkCloseX == 1) {

                    //alert('1111');
                    $.ajax({
                        type: "GET",
                        url: "Account/SetAuditHeaderTimeOut",
                        dataType: "json",
                        success: function (result) {
                            if (result != null) {
                            }
                        }
                    });
                }
            }
        };
  });

Combine the mousemove and window.onbeforeunload event :-

Note
Rectangle 27 0

javascript Detect Close windows event by Jquery?


$(window).unload(function(){
alert("Goodbye!");
});
window.onbeforeunload = function() {
    return "Bye now!";
};

In Firefox the custom messages does not work.

The unload() method was deprecated in jQuery version 1.8.

and if you are using 1.8 and higher

so if you are using versions older than 1.8

Note