Reputation: 225
I want to Detect user activity, when the user click the browser [x]. It dosn't matter if I use java scripts or without javascript as long as I perform it on the serverside (codebehind) because I have a process that I need to accomplish on a serverside before the page is closing.
Upvotes: 0
Views: 997
Reputation: 4363
You cannot know for sure if the user is closing the browser (pressing [x]) or not. You can only know whether the user is leaving a certain page. You can capture that with the window.onunload
event. This event will fire when the user presses [x], but it will also fire when the user clicks a link on the page. That's what I mean when I say you can't know whether the user clicked [x] for sure. It might still do the job for you though.
Some details about the onunload event can be found here. You can execute a fire-and-forget ajax call to the server to do whatever you need to do serverside.
window.onunload = function(e) {
$.ajax({
async: false, // You need this to make sure the AJAX call will complete
type: 'POST',
url: '/some/url'
});
};
Upvotes: 1