Detecting close button through X and ALT-F4 combination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raknin
    New Member
    • Oct 2007
    • 82

    Detecting close button through X and ALT-F4 combination

    Hi,

    I want to detect when the user close a window through the X button or ALT-F4 or
    when the user go to another page and leave my page. I try to use the onuload and on unloadbeore
    but it works on ie 6 and Firefox 2 and it doesn't work on Safari 3.03 and Opera 9.2, it is also doesn't solve the problem when the user navigate to another page through the same windows, Any Ideas?

    Thanks
    Ronen
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I assume you mean onbeforeunload. That's supported in Firefox and IE, not sure about Safari. Opera doesn't require it unless you're closing the browser because the scripts remain running when you go back to the page if you only navigate away with back/forward. There's no reliable cross-browser way to differentiate between navigating away and the close event. Why do you need to detect it?

    Comment

    • raknin
      New Member
      • Oct 2007
      • 82

      #3
      Hi Acoder

      Nice to hear from you again. The reason that I need it is because i want to close some records in the DB that indicate when the user leave the site. I tried the onbeforeunload but it did not solve the problem when the user navigate to a new windows on opera or safari. This is why I need it.

      Another question can I have an event listener in the background that can catch key
      sroke like ALT+F4.

      I did soem research and found that ie6 and FF2 master the onunload and onbeforeunload completly. While opera 9 doesn't support it at all an safari 3 support only the navigating action and not the close(x) and ALT-F4.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        What if the user accidentally went back one page (can be triggered by the backspace key)? I would use a timeout instead. After a period of inactivity, the user is automatically signed out, i.e. if he returns to your site, you can compare the timestamp and require the user to sign in again.

        Comment

        • raknin
          New Member
          • Oct 2007
          • 82

          #5
          Originally posted by acoder
          What if the user accidentally went back one page (can be triggered by the backspace key)? I would use a timeout instead. After a period of inactivity, the user is automatically signed out, i.e. if he returns to your site, you can compare the timestamp and require the user to sign in again.
          It can be done if you run a cron job that sign-out users automatically after T minutes. You must keep in mind that the user can click the back button and never return to the site again this is also possibility.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            True, though you don't need to worry about that if no-one visits again. It depends on what you're storing in the database for the user.

            onbeforeunload should really only be used as a convenience, not as an annoyance.

            Comment

            • raknin
              New Member
              • Oct 2007
              • 82

              #7
              Thanks acorder for you help.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                No problem. Did you manage to solve your problem?

                Comment

                • raknin
                  New Member
                  • Oct 2007
                  • 82

                  #9
                  Originally posted by acoder
                  No problem. Did you manage to solve your problem?
                  Not really, I have another problem, maybe you know the solution. How can I detected which link was pressed in the unload function before jumping to this link.

                  I have an unload function that doing some actions. I want this unload function to be be activated only if the link that we jump to is not in some links list.
                  Any suggestions?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You can't detect which URL the user will navigate to. That would be a lack of security/privacy and could be misused.

                    However, if it's via clicking a link on your site, you can add some code onclick for the links that match those that you have in the links array/list.

                    Comment

                    Working...