在关闭标签时询问

时间:2017-09-12 19:33:34

标签: javascript jquery

我想在关闭我的页面时询问用户。我试过这个脚本:

<script language="JavaScript" type="text/javascript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        return "alert";
    }
</script>

<script type="text/javascript">
  var areYouReallySure = false;
  function areYouSure() {
    if(allowPrompt){
        if (!areYouReallySure && true) {
            areYouReallySure = true;
            var confMessage = "***************************************\n\n W A I T !!! \n\nBefore leaving our site, follow CodexWorld for getting regular updates on Programming and Web Development.\n\n\nCLICK THE *CANCEL* BUTTON RIGHT NOW\n\n***************************************";
            return confMessage;
        }
    } else {
        allowPrompt = true;
    }
  }

var allowPrompt = true;
window.onbeforeunload = areYouSure;
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(window).on('beforeunload', function() {
    return "You should keep this page open.";
  });
</script>

我读到在现代浏览器中脚本可能无效。有没有其他方法可以解决这个问题?

0 个答案:

没有答案