There is no reliable/standard way of detecting a browser close event. Though there are non-supported workarounds, a browser close event is not something you should rely on.
Anyways, here is a snippet which seems to be working fine on IE. If you are new to Ajax, I recommend using the
AjaxToolbox library to do away with writing most of the boilerplate code.
<!--
Make async request when a browser window is closed.
Copyright (C) 2008 sos aka Sanjay
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Script-Content-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example</title>
<script type="text/javascript" src="AjaxRequest.js"></script>
<script type="text/javascript">
// Error checking omitted for brevity
function doIt() {
var o = {
"url": "http://your.domain.net/logout",
"onSuccess": function(req) {
alert("status: " + req.status + "\nStatus text: " + req.statusText);
},
"onError": function(req) {
alert('Error!\nStatusText='+req.statusText+'\nContents=' +
req.responseText);
}
};
AjaxRequest.get(o);
alert("Request sent; check server logs....");
}
window.onunload = doIt;
</script>
</head>
<body id="bdy">
<p>Close this window to test forced browser expiry.</p>
</body>
</html>
Last edited by ~s.o.s~; Nov 22nd, 2008 at 10:14 am.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
Offline 8,871 posts
since Jun 2006