Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    user closing browser event?

    Is there a way to capture the event of the user closing the browser??

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    Not really. You can capture the onunload but that can happen for many reasons, including even simply moving to another page on your own site. And there's really no guarantee the onunload will occur if, for example, the user just kills the browser process from the task manager (or maybe turns off the power switch or unplugs the network cable or...).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    Does it have to be used with the BODY tag or can I use it elsewhere. Can you maybe provide a small example.

    Tracy

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,078
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by nikko50 View Post
    Does it have to be used with the BODY tag or can I use it elsewhere. Can you maybe provide a small example.

    Tracy
    Wel, you can only onload the whole page, not a a part of it!

    <body onunload = 'alert("Thank you for visiting my site!")'>

    But as Old Pedant says, it is far from reliable. And some people see that sort of thing as intrusive, with overtones of being monitored. Not recommended generally.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    You can, of course, simply attach it in the JS code. But what you need to do is carefully exclude normal navigation so that the user doesn't get an annoying message when, for example, submitting a <form>.

    And there is also onbeforeunload which might be more useful.

    Example at http://www.plopon.com/byebye.html

    And the code for it:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var trap = true;
    
    function byebye( )
    {
        if ( ! trap ) return null;
        return "Are you sure you really want to leave my lovely site???";
    }
    window.onbeforeunload = byebye;
    </script>
    </head>
    <body>
    <a href="rollone.html" onclick="trap=false;return true;">Go here on my site</a>
    <hr>
    <a href="http://www.cnn.com">Go away</a>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •