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