Avatar of winsoftech
winsoftech
 asked on

Detecting when Firefox browser window is closed

above code is running only IE

I need code for Firefox and other famous browsers


Please solve it
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<script type="text/javascript">
  function logout()
  {
    if(window.event.clientX<0 && window.event.clientY<0)
	{
       window.open('../logout.php');
		
    }
  }
</script>
<body  onUnload="logout();" >
 
</body>
</html>

Open in new window

JavaScript

Avatar of undefined
Last Comment
winsoftech

8/22/2022 - Mon
Morcalavin

There is no cross browser way to reliably detect if the browser window is closed, only if the page has been unloaded.  The code you provided would not work in IE7 or any other browser(as you already found out).  Also, most popup blockers would disallow the popup, since it was unrequested by the user.
What does logout.php actually do?  Does it just show a logout message to the user? Or does it actually run logout code do remove a cookie or whatever?
winsoftech

ASKER
Ok

how to update our database user logout infomation
Morcalavin

Well, you could send an ajax request to the logout.php page.  However, if all you are calling is a GET request, and if you don't care what the return value is from the logou.php page, you could create an Image object, and set the src to the logout.php page.

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<script type="text/javascript">
  function logout() {
    var get = new Image();
    get.src = '../logout.php?' + Math.random;
    alert('You have been logged out.');
  }
</script>
<body onunload="logout();" >
 
</body>
</html>

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Morcalavin

It's worth noting that the problem with this is that it will occur whenever they navigate away from the page, not just when they close the browser.
Morcalavin

Sorry for the triple post, but if you are using cookies and you want the session to end when they close the browser, you can set the cookie so that it expires whenever they close the browser window.
ASKER CERTIFIED SOLUTION
winsoftech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
winsoftech

ASKER
ok
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.