
August 22nd, 2009, 08:18 AM
|
|
Contributing User
|
|
Join Date: Nov 2007
Posts: 154
Time spent in forums: 17 h 16 m 9 sec
Reputation Power: 3
|
|
Ajax - Onbeforeunload => Logout If Closed
Working solution to logout a user if they didn't click the Logout button/link
Works in IE only, any ideas?
PHP Code:
window.onbeforeunload = confirmExit;
var aClick=false;
function confirmExit(e) {
if(document.all)e = event;
if(!e)e=window.event;
if (e) {
if(aClick==false && (e.target==document || e.clientX<0 || e.clientY<0)) {
//alert("Loggined out..."); //remove don't want an alert box!
// add your server call to logout the user
ajaxCall(open_win());
function open_win() {
ajaxCall("http://example.com/logout.php");
//or you could call a popup; window.open("http://example.com/logout.php"); //actually irritating!
}
function ajaxCall(dname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", dname, false);
xmlDoc.send("");
//alert(xmlDoc.responseText); //removed due to blank alert box!
//return xmlDoc.responseXML; //removed, no clue why!
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(dname);
//alert(xmlDoc.responseText); //removed due to blank alert box!
//return xmlDoc; //removed, no clue why!
}
//alert("Error loading document"); //removed due to blank alert box!
return null;
}
}
}
}
__________________
Amateurs ... Built the ark.
Professionals ... Built the Titanic.
|