Catching someone leaving the site

Discussion in 'JavaScript' started by annunaki10, Oct 29, 2008.

  1. #1
    I recently acquired an old site with a high bounce rate. I'm adding a lot of new stuff to the site and want to point people to it. So when they try to navigate away, i want to add a confirm prompt and tell them to check out the new stuff. While i know this is annoying, i want to have it up for a short period of time.

    I know i can put a check on the onunload but how do i prevent the page from changing? By the time onunload runs, it's already leaving.

    Thanks for your help.
     
    annunaki10, Oct 29, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    you need an event handler.

    for example, mootools:
    
    <script type="text/javascript">
        // attach a key listener
        Element.NativeEvents.beforeunload = 2;
        window.addEvent("beforeunload", function(e) {
            if (confirm("Are you certain you want to do this? Please stay...")) {
                e.preventDefault();
            }
        });
    </script>
    
    PHP:
    but it can work w/o it also.

    mind you, only modern browsers support beforeunload, rest is 'unload' which fires a little too late to stop it...
     
    dimitar christoff, Oct 30, 2008 IP
  3. annunaki10

    annunaki10 Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    That block of code didn't work. I did find a block that does
    
    <script language = "javascript">
    function goodbye(e) 
    {
        if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
    
        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) 
        {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    window.onbeforeunload = goodbye;
    </script>
    
    PHP:
    The problem is this works when leaving the page. I want something that works only when trying to leave the site. If someone is going from one page to another, i don't want to bother them. I only want to bother them if they're going off to a new site.

     
    annunaki10, Oct 30, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    what i posted is working but relies on the mootools js framework.

    but i see the problem you are having--and frankly, it is impossible to know where they are headed, hence you can't tell if they are leaving your site or just moving between pages. you need to think of a better way of managing your bounce rate to prevent them from trying to leave in the first place....
     
    dimitar christoff, Oct 30, 2008 IP
  5. annunaki10

    annunaki10 Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    Yeh that's what i was afraid of. I've already started bring bounce down by adding new content and chaing the look of the site. Thanks for the help though.
     
    annunaki10, Nov 12, 2008 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I would avoid doing this if at all possible. You are MORE likely to have people NEVER return to your site if you do this sort of thing than it is to increase your retention.

    Goofy navigation tricks won't retain a userbase any more than a truckload of SEO trickery, stupid flash tricks or all those pretty pictures the nimrods who draw a .psd and have the nerts to call themselves designers churn out.

    It's all about the content.
     
    deathshadow, Nov 12, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    True, unless there is a reason why you are telling him to stay.

    (gmail warns you if you leave when a ajax request is pending)
     
    MMJ, Nov 13, 2008 IP
  8. annunaki10

    annunaki10 Active Member

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #8
    yeh i figured that much. i was really against the idea of doing it so it's probably for the best that i cant keep track that specifically.
     
    annunaki10, Nov 13, 2008 IP