473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to Detect the Client Browser/Tab Closed

India777
Hai all,
How to Detect, the Client Closed the Browser/Tab using Javascript.
May 24 '12 #1

✓ answered by Dormilich

I would register the logged-in user as soon as he logs in, not when he logs out.

another problem is also, what if the user closes the tab and 5 minutes later opens a page from your site. do you consider him logged-out and force him to log-in again?

maybe there’s a way to tell when C# destroys a session to use that as log-out time?

you could also save the time of the last action (Request of a page) which is often not too far away from leving your site.

15 128203
Dormilich
8,658 Expert Mod 8TB
the unload & beforeunload events are fired in that case.
May 24 '12 #2
This events also fired when i Refresh the Page. What i do?
May 24 '12 #3
Killer42
8,435 Expert 8TB
Isn't reloading effectively the same as closing and reopening the page?
May 27 '12 #4
I am only need to Find Browser/Tab Close only. Not an Reload or Refresh.I know this is not Possible Easily with Javascript.
May 28 '12 #5
Killer42
8,435 Expert 8TB
I'm no expert in this area, so hopefully we can get some input from someone more knowledgeable.

But it seems likely to me that when the user refreshes the page, what happens is that the page is unloaded (triggering beforeunload and unload events), then loaded again from scratch. From the viewpoint of javascript code within that page, it would be the same as if the user closed the page then opened a fresh copy of it.

Someone please correct me if I'm off track here.
May 28 '12 #6
Dormilich
8,658 Expert Mod 8TB
you’re absolutely correct. due to the stateless nature of HTTP, there is no way for JavaScript itself to differentiate between a (user triggered) page close or reload.
May 28 '12 #7
acoder
16,027 Expert Mod 8TB
India777, why do you need to detect this and what are you trying to do?

The question you've asked has already been answered, but you're probably trying to solve something else and thought you needed to be able to detect the browser/tab closing to solve it.
May 30 '12 #8
When the user Close his/her Browser without Logout, I need to insert the row in the table.My Application is Created using Asp.net with c#.
May 30 '12 #9
Dormilich
8,658 Expert Mod 8TB
When the user Close his/her Browser without Logout, I need to insert the row in the table
what row?
May 30 '12 #10
Insert row in the Database Table, Containing the fields Username (Name of the User who logged in) and DateTime
May 30 '12 #11
Dormilich
8,658 Expert Mod 8TB
I would register the logged-in user as soon as he logs in, not when he logs out.

another problem is also, what if the user closes the tab and 5 minutes later opens a page from your site. do you consider him logged-out and force him to log-in again?

maybe there’s a way to tell when C# destroys a session to use that as log-out time?

you could also save the time of the last action (Request of a page) which is often not too far away from leving your site.
May 30 '12 #12
In c# the Session_End (Global method) will call after some times when user Closed their Browser/Tab. So i write my insert Query inside this Method.

Thanks very much Dormilich,Killer42,acoder.
again thanks for spend your time to Solve my Problem.
May 30 '12 #13
checks if the X button was clicked on browser window


The following code returns true if user clicks on Close button else false. This code you can use it on events window.unload or window.onbeforeunload

Expand|Select|Wrap|Line Numbers
  1. function isUserClickedCloseButton()
  2.     {
  3.  
  4.         var browserWindowWidth = 0;
  5.         var browserWindowHeight = 0;
  6.         // gets the width and height of the browser window
  7.         if (parseInt(navigator.appVersion) > 3)
  8.         {
  9.         if (navigator.appName == "Netscape")
  10.         {
  11.             browserWindowWidth = window.innerWidth;
  12.             browserWindowHeight = window.innerHeight;
  13.         }
  14.         if (navigator.appName.indexOf("Microsoft") !=- 1)
  15.         {
  16.             browserWindowWidth = top.window.document.body.offsetWidth;
  17.             browserWindowHeight = top.window.document.body.offsetHeight;
  18.         }
  19.         }
  20.         // checks if the X button was closed
  21.         // if event.clientY < 0, then click was on the browser menu area
  22.         // if event.screenX > (browserWindowWidth - 25), the X button was clicked
  23.         // use screenX if working with multiple frames
  24.         return (event.clientY < 0 && event.screenX > (browserWindowWidth - 25)) ? true : false;
  25.     }
Aug 23 '13 #14
Also looking for the problem. I had just found how to solve this problem


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Detecting browser close in IE</title>
  4.   <script type="text/javascript" src="/jquery/1.4.4/jquery.min.js"></script>
  5. <script type="text/javascript">
  6. var validNavigation = false;
  7.  
  8. function wireUpEvents() {
  9.   var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
  10.   var leave_message = 'ServerThemes.Net Recommend BEST WEB HOSTING at new tab window. Good things will come to you'
  11.   function goodbye(e) {
  12.     if (!validNavigation) {
  13.     window.open("/best-web-hosting-services","_blank");
  14.         return leave_message;
  15.  
  16.     }
  17.   }
  18.   window.onbeforeunload=goodbye;
  19.  
  20.   // Attach the event keypress to exclude the F5 refresh
  21.   $(document).bind('keypress', function(e) {
  22.     if (e.keyCode == 116){
  23.       validNavigation = true;
  24.     }
  25.   });
  26.  
  27.   // Attach the event click for all links in the page
  28.   $("a").bind("click", function() {
  29.     validNavigation = true;
  30.   });
  31.  
  32.   // Attach the event submit for all forms in the page
  33.   $("form").bind("submit", function() {
  34.     validNavigation = true;
  35.   });
  36.  
  37.   // Attach the event click for all inputs in the page
  38.   $("input[type=submit]").bind("click", function() {
  39.     validNavigation = true;
  40.   });
  41.  
  42. }
  43.  
  44. // Wire up the events as soon as the DOM tree is ready
  45. $(document).ready(function() {
  46.   wireUpEvents();
  47. });
  48. </script>
  49. </head>
  50. <body>
  51. <p>
  52. Check which action detects browser window close:
  53. <ol>
  54. <li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
  55. <li>Navigate away from this page through a <a href="http://serverthemes.net/">link</a></li>
  56. <li>Type another URL in the address bar</li>
  57. <li>Click Back or Forward button</li>
  58. <li>Click the Close (X) button in the top-rightmost corner of the browser</li>
  59. <li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
  60. <li>Press Alt+F4 key</li>
  61. </ol>
  62. </p>
  63. <p>In IE, the last 3 actions are correctly detected by the <a href="#" onclick="alert(doUnload);return false">javascript code</a> inside this page as browser close.</p>
  64. </body>
  65. </html>
Mar 30 '15 #15
Killer42
8,435 Expert 8TB
Thanks ServerThemes. Although it's probably a bit late to help India777, any information added here is potentially helpful next time someone comes along with a similar question.
Apr 4 '15 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: chris | last post by:
I'd like to initiate a php script using a standard httpd request, but then allow the client browser to disconnect without terminating the script. Freeing up the browser to access other functions...
5
by: Chris Fynn | last post by:
Is there an easy way to detect the local language settings of a client browser or system? I want to for example determine if the users browser or system is set to English, French, or Italian...
1
by: onewebclick | last post by:
Is there a way to detect a browser cache is full using javascript or HTML thorugh a web page and inform the user to clear the cache to improve performance of the website. It looks like google's...
7
by: Chris | last post by:
Can anybody tell me how to detect is the user has closed the browser Thank you
0
by: dotnettester | last post by:
Hi, I want to detect if the clients browser has cookies/javascript disabled. I know the class 'HttpBrowserCapabilities' but it gives information about the capabilities of client browser not the...
0
by: sahaja108 | last post by:
Hello. I was wondering if it possible to detect a Browser Back/Forward by PHP. I mean. I am in page A, I go to page B and then I click on the Back button and I return to A. Does it exist a php...
11
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have an aplication web asp.net 2.0 and I am Trying to detect the close event in browser, which is the best performance for do this ? What's up with Alt+F4, Refresh, user clicks X,...
1
by: mmr315 | last post by:
how to destroy session when browser closed with out logout.pls hlp me
1
by: soni2926 | last post by:
hi, i'm working on a site, which has a session created after a user logins, each page checks to make sure the session is there onload otherwise redirects to the login page. now the clients are...
2
by: tokcy | last post by:
Hi All, I want to kip session alive until i closed the browser. Default session time 30 minutes and if we are using session.gc_maxlifetime then i have to give specific time like 8 hrs or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.