How to Detect the Client Browser/Tab Closed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • India777
    New Member
    • Apr 2012
    • 61

    How to Detect the Client Browser/Tab Closed

    Hai all,
    How to Detect, the Client Closed the Browser/Tab using Javascript.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the unload & beforeunload events are fired in that case.

    Comment

    • India777
      New Member
      • Apr 2012
      • 61

      #3
      This events also fired when i Refresh the Page. What i do?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Isn't reloading effectively the same as closing and reopening the page?
        Last edited by Killer42; May 27 '12, 01:01 AM. Reason: Slight rewording

        Comment

        • India777
          New Member
          • Apr 2012
          • 61

          #5
          I am only need to Find Browser/Tab Close only. Not an Reload or Refresh.I know this is not Possible Easily with Javascript.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            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.
            Last edited by Killer42; May 28 '12, 12:27 PM. Reason: Slight rewording.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              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.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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.

                Comment

                • India777
                  New Member
                  • Apr 2012
                  • 61

                  #9
                  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#.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    When the user Close his/her Browser without Logout, I need to insert the row in the table
                    what row?

                    Comment

                    • India777
                      New Member
                      • Apr 2012
                      • 61

                      #11
                      Insert row in the Database Table, Containing the fields Username (Name of the User who logged in) and DateTime

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        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.
                        Last edited by Dormilich; May 30 '12, 01:27 PM.

                        Comment

                        • India777
                          New Member
                          • Apr 2012
                          • 61

                          #13
                          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,Kille r42,acoder.
                          again thanks for spend your time to Solve my Problem.

                          Comment

                          • Srikanth7
                            New Member
                            • Aug 2013
                            • 1

                            #14
                            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.onbefore unload

                            Code:
                            function isUserClickedCloseButton()
                            	{
                            	    
                            	    var browserWindowWidth = 0;
                            	    var browserWindowHeight = 0;
                            	    // gets the width and height of the browser window
                            	    if (parseInt(navigator.appVersion) > 3)
                            	    {
                            		if (navigator.appName == "Netscape")
                            		{
                            		    browserWindowWidth = window.innerWidth;
                            		    browserWindowHeight = window.innerHeight;
                            		}
                            		if (navigator.appName.indexOf("Microsoft") !=- 1)
                            		{
                            		    browserWindowWidth = top.window.document.body.offsetWidth;
                            		    browserWindowHeight = top.window.document.body.offsetHeight;
                            		}
                            	    }
                            	    // checks if the X button was closed
                            	    // if event.clientY < 0, then click was on the browser menu area
                            	    // if event.screenX > (browserWindowWidth - 25), the X button was clicked
                            	    // use screenX if working with multiple frames
                            	    return (event.clientY < 0 && event.screenX > (browserWindowWidth - 25)) ? true : false;
                            	}
                            Last edited by Dormilich; Aug 23 '13, 06:59 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

                            Comment

                            • ServerThemes
                              New Member
                              • Mar 2015
                              • 1

                              #15
                              How to Detect the Client Browser/Tab Closed

                              Also looking for the problem. I had just found how to solve this problem


                              Code:
                              <html>
                              <head>
                              <title>Detecting browser close in IE</title>
                                <script type="text/javascript" src="/jquery/1.4.4/jquery.min.js"></script>
                              <script type="text/javascript">
                              var validNavigation = false;
                               
                              function wireUpEvents() {
                                var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
                                var leave_message = 'ServerThemes.Net Recommend BEST WEB HOSTING at new tab window. Good things will come to you'
                                function goodbye(e) {
                                  if (!validNavigation) {
                              	window.open("/best-web-hosting-services","_blank");
                              		return leave_message;
                                   
                                  }
                                }
                                window.onbeforeunload=goodbye;
                               
                                // Attach the event keypress to exclude the F5 refresh
                                $(document).bind('keypress', function(e) {
                                  if (e.keyCode == 116){
                                    validNavigation = true;
                                  }
                                });
                               
                                // Attach the event click for all links in the page
                                $("a").bind("click", function() {
                                  validNavigation = true;
                                });
                               
                                // Attach the event submit for all forms in the page
                                $("form").bind("submit", function() {
                                  validNavigation = true;
                                });
                               
                                // Attach the event click for all inputs in the page
                                $("input[type=submit]").bind("click", function() {
                                  validNavigation = true;
                                });
                               
                              }
                               
                              // Wire up the events as soon as the DOM tree is ready
                              $(document).ready(function() {
                                wireUpEvents();
                              });
                              </script>
                              </head>
                              <body>
                              <p>
                              Check which action detects browser window close:
                              <ol>
                              <li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
                              <li>Navigate away from this page through a <a href="http://serverthemes.net/">link</a></li>
                              <li>Type another URL in the address bar</li>
                              <li>Click Back or Forward button</li>
                              <li>Click the Close (X) button in the top-rightmost corner of the browser</li>
                              <li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
                              <li>Press Alt+F4 key</li>
                              </ol>
                              </p>
                              <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>
                              </body>
                              </html>

                              Comment

                              Working...