View Single Post
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: deleting sessions on closing browser tab

 
0
  #2
Nov 22nd, 2008
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--
  2. Make async request when a browser window is closed.
  3.  
  4. Copyright (C) 2008 sos aka Sanjay
  5.  
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. -->
  19. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  20. "http://www.w3.org/TR/html4/strict.dtd">
  21. <html>
  22. <head>
  23. <meta http-equiv="Script-Content-Type" content="text/javascript">
  24. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  25. <title>Example</title>
  26. <script type="text/javascript" src="AjaxRequest.js"></script>
  27. <script type="text/javascript">
  28. // Error checking omitted for brevity
  29. function doIt() {
  30. var o = {
  31. "url": "http://your.domain.net/logout",
  32. "onSuccess": function(req) {
  33. alert("status: " + req.status + "\nStatus text: " + req.statusText);
  34. },
  35. "onError": function(req) {
  36. alert('Error!\nStatusText='+req.statusText+'\nContents=' +
  37. req.responseText);
  38. }
  39. };
  40. AjaxRequest.get(o);
  41. alert("Request sent; check server logs....");
  42. }
  43. window.onunload = doIt;
  44. </script>
  45. </head>
  46. <body id="bdy">
  47. <p>Close this window to test forced browser expiry.</p>
  48. </body>
  49. </html>
Last edited by ~s.o.s~; Nov 22nd, 2008 at 10:14 am.
I don't accept change; I don't deserve to live.
Reply With Quote