1

Kindly help me, how can i detect only browser / tab close button event in javascript or jquery. I mean only close button no page refresh and any other menu link opening etc.

I tried this code on onbeforeunload event of javascript

function handleBrowserCloseButton(event) {

   if(window.event.clientX < 0 && window.event.clientY <0)
    {
      alert('Browser close button clicked');    
    }
} 

But unfortunately it did not solved my problem. Kindly help me.

Software Company
  • 151
  • 4
  • 15

1 Answers1

0

In my understanding, you want to fire a function when the close button of the browser is clicked.

In jQuery:

$(window).on('beforeunload', function () {
    console.log("leaving");
    return "sure to leave?";
});

In raw javascript:

window.onbeforeunload = function (event) {
    console.log("leaving");
    return confirm("sure to leave?");
}

However, what kind of code can be executed in the onbeforeunload event handler is not very clear:

$(window).on('beforeunload', function () {
    console.log("leaving");
    alert("leaving");
    return "sure to leave?";
});

Here, the console.log code works, while the alert code doesn't.

Halt
  • 1,055
  • 9
  • 18