Popup when leaving website

六眼飞鱼酱① 提交于 2020-01-01 18:17:06

问题


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>

回答1:


Try this:

window.onbeforeunload = popup;

function popup() {
  return 'I see you are leaving the site';
}


来源:https://stackoverflow.com/questions/11005887/popup-when-leaving-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!