Vinoth Babu
Vinoth Babu

Reputation: 21

How to detect browser close event in Firefox?

How to detect browser close event in Firefox browser? I want to do some clean up process in server side and we maintaining last logout time. To achieve this need fire an ajax call when the user clicks logout or the browser is closed. For IE the following code is working:

if (window.event.clientY < 0 && (window.event.clientX > (document.documentElement.clientWidth - 5) || window.event.clientX < 0)) {

        logoutUser();//logging out the user

        } 
}


function logoutUser(){

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
    //Safari
    xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
xmlhttp.open("GET", "Logout.action", true);
xmlhttp.send();

}

How to do this in Firefox? Please help me.

Upvotes: 2

Views: 9025

Answers (3)

james li
james li

Reputation: 184

window.addEventListener('unload',fn,false);

Upvotes: 0

Sarath
Sarath

Reputation: 9156

Use onbeforeunload

window.onbeforeunload = function (e) {

  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Safari
  return 'Any string';
};

Upvotes: 6

moe
moe

Reputation: 29704

var closedWindow = function() {
    //code goes here
};

if (window.addEventListener) {
    window.addEventListener('unload', closedWindow, false);
} else if (window.attachEvent) {
    // pre IE9 browser
    window.attachEvent('onunload', closedWindow);
}

Upvotes: 0

Related Questions