I use a pretty standard exit pop script I picked up a long time ago. I just realized that it only actually loads the exit pop page correctly on Firefox (on mac) and IE (on Windows).
Most browsers don't show the correct Alert text (WAIT!!! Don't leave, etc). It just asks if you want to "Stay on Page" or "Leave page".
Seems that:
Windows Firefox: Only says to either "Stay on Page" or "Leave page" but does redirect
Windows Chrome: Only says to either "Stay on Page" or "Leave page" but does NOT redirect
Windows IE: Works Perfectly
Mac Safari: "Stay on Page" or "Leave page" but does NOT redirect
Mac Chrome: "Stay on Page" or "Leave page" but does NOT redirect
Mac Firefox: Works Perfectly
Does anyone know if there is an updated script that will work properly on more browsers? I'm sure I'm leaving money on the table.
Below is my current code. Thanks!
<script type="text/javascript" language="javascript">
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if(allowPrompt)
{
if (!areYouReallySure && !internalLink && true) {
areYouReallySure = true;
location.href="http://url.com"
return "WAIT!!\n\nAre you sure you want to leave?!\n\nClick cancel to claim your product now.\n\nClick ***CANCEL*** now!\n\n";
}
}
else
{
allowPrompt = true;
}
}
var allowPrompt = true;
window.onbeforeunload = areYouSure;
function NoPrompt()
{
allowPrompt = false;
}
</script>
This (popping a custom message) turns out to be a non-trivial problem: Firefox, in particular, is no longer very enthusiastic about custom exit popups.
This JQuery code seems to work - note that you'll need to add the logic to not pop the popup when users click a link. (Here's a good guide to that) :
$(window).bind('beforeunload', function() {
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
if(confirm("Are you Sure do you want to leave?")) {
history.go();
} else {
window.setTimeout(function() {
window.stop();
}, 1);
}
} else {
return "Are you Sure do you want to leave?";
}
});
Thanks for the link. I just went through it all and tested - now the custom exit pop text is working --- BUT --- still can't seem to get the page to redirect on exit pop.
Example Flow:
<script type="text/javascript">
var popit = true;
window.onbeforeunload = function() {
if(popit == true) {
popit = false;
location.href="http://msn.com"
return "WAIT!! Don't leave?\n\nClick ***CANCEL*** and don't miss out on these great offers!\n\n";
}
}
</script>
Redirect in the onbeforeunload is disabled in i think chrome, safari and even firefox / IE10 now. You can still do a div overlay (ajax), which basically means you will end up loading another page and load the overlay in the onbeforeunload function. Here's some inspiration: http://www.andysowards.com/blog/2008...-on-page-exit/
About the message only showing Stay on page or leave page options. That occurs with Firefox, chrome and safari if i recall correctly, you can solve that by adding a alert() before your return "blabl" line. In some browsers this will like double serve the message though (IE will show the alert, and the text in your return "blabla" line).