Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup when leaving website

I got a problem with JavaScript. I want a script that will pop-up on exit whole web-site a message with question and if visitor answers "NO" web page closes and if he answers "YES" he will be redirected to another page. I found a example at http://www.pgrs.net/2008/01/30/popup-when-leaving-website/ but it seems that it doesnt work for me. I couldnt find any solution. Pleae check my code and tell me maybe i'm doing something wrong ? Here`s my source code.

Maybe somebody will see a problem.

 <!DOCTYPE html>
<html lang="lt">
<head>
    <meta charset="utf-8">
    <title>PUA.LT</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="">
    <meta name="author" content="Perfect WEB Solutions">
    <link rel="stylesheet" href="<?php echo base_url("additional/style.css") ?>">
    <script src='<?php echo base_url("additional/prototype.js")?>' type='text/javascript' ></script>
</head>
<body>
<script type="text/javascript">


Event.observe(document.body, 'click', function(event) {
  if (Event.element(event).tagName == 'A') {
    staying_in_site = true;
  }
});

window.onunload = popup;

function popup() {
  if(staying_in_site) {
    return;
  }
  alert('I see you are leaving the site');
}
</script>

</body>
</html>
like image 965
Daniil T. Avatar asked Dec 20 '22 20:12

Daniil T.


1 Answers

Try this:

window.onbeforeunload = popup;

function popup() {
  return 'I see you are leaving the site';
}
like image 111
emphaticsunshine Avatar answered Dec 23 '22 11:12

emphaticsunshine