0

I have an exit popup js function which displays an alert and adds something to the url (and redirects) when someone tries to leave the page.

The alert is displayed in all browsers but the code:

window.location.href = "?p=exit" 

is not executed in Chrome and IE.

It works fine in Firefox. When you reload the page an alert is displayed and the url is modified.

Take a look at the source code, it is very simple.

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
var exit=true;
    function confirmExit()
            {
            if(exit)
            {
            window.location.href = "?p=exit";
            }
            if(exit)
            return "Wait! Don't Leave Empty Handed!\n\nThank you for taking the time to check out our offer! Before you go we have a complimentary crash to help you succeed. Click the 'Cancel' or 'Stay On This Page' button if you're interested!";
            }
</script>
</head>
<body  onbeforeunload="return confirmExit()">
</body>
</html>
Zolka
  • 49
  • 1
  • 9

1 Answers1

1
  • You cannot (cross-browser) redirect from "onbeforeunload".
  • Chrome blocks alerts set in "onbeforeunload".

Check out this answer for more information: https://stackoverflow.com/a/7080331/353710

Community
  • 1
  • 1
Nick Stemerdink
  • 1,067
  • 9
  • 22