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.s开发者_如何学Pythonend();
}
How to do this in Firefox? Please help me.
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';
};
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);
}
window.addEventListener('unload',fn,false);
精彩评论