need a solution when window is closed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • libra10581
    New Member
    • Mar 2007
    • 1

    need a solution when window is closed

    Hi ,

    In my project i need to maintain online users. But some one close the browser with out clicking the logout. So i face the problem to find who is in online currently. Plz tell me the way how to check that user is closed the window or not.

    I wrote the code in onunload event. But that event is called when i click the refresh button or i moved any other location. So plz give the solution for me.

    Thanks,
    Raja Mohamed.S
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Originally posted by libra10581
    Hi ,

    In my project i need to maintain online users. But some one close the browser with out clicking the logout. So i face the problem to find who is in online currently. Plz tell me the way how to check that user is closed the window or not.

    I wrote the code in onunload event. But that event is called when i click the refresh button or i moved any other location. So plz give the solution for me.

    Thanks,
    Raja Mohamed.S
    What do you have coded thus far?

    Comment

    • SanthuBalu
      New Member
      • Mar 2007
      • 4

      #3
      Originally posted by libra10581
      Hi ,

      In my project i need to maintain online users. But some one close the browser with out clicking the logout. So i face the problem to find who is in online currently. Plz tell me the way how to check that user is closed the window or not.

      I wrote the code in onunload event. But that event is called when i click the refresh button or i moved any other location. So plz give the solution for me.

      Thanks,
      Raja Mohamed.S

      Hi,
      To find whether the user has closed the window without clicking logout, i included the following lines in my script. This would track the window close regardless of its status(Window close in maximised state or window close in minimised state)

      <script language="JavaS cript">
      function initUnload()
      {
      var top=self.screen Top;
      if (top>9000)
      alert("window closed"+top);
      else if(top<0)
      alert("window closed"+top);
      }
      window.onunload = initUnload;
      </script>

      Thanks,
      Santhu Balu

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        On a project I'm currently working on, I needed to record a users last access time when they left the website, either by logging out, closing the browser, or visiting another website. You want to use the onbeforeunload event. I had this run a javascript function to make a synchronous (ajax) call to a PHP script which did the work. Using the onbeforeunload event will do what you want.

        My code, if it helps:

        Code:
            // add event listener
            function addListener(obj, event, func) {
              var obj = (typeof obj == 'string' ? d.getElementById(obj) : obj);
              if (obj && obj.addEventListener) {
                obj.addEventListener(event, func, false);
              } else if (obj && obj.attachEvent) {
                obj.attachEvent('on'+event, func);
              }
            }
            
            // return AJAX object
            function getHttpXml() {
              if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
              } else if (window.ActiveXObject) {
                try {
                  return new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                  try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e2) { } // no Ajax support
                }
              } return null;
            }
            
            addListener(window, 'beforeunload', bye);
            var xmlHttp = getHttpXml();
            
            function bye() {
              if (xmlHttp) {
                var url = "script.php?action=leaving";
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
              }
            }
        You can add this in the head of your html page, if you want, just edit the relevant line to change the filename / parameters you want to call. I hope that helps.

        Comment

        Working...