离开我的网站显示带有消息的弹出框

时间:2015-11-20 05:28:18

标签: javascript php jquery

当有人试图离开我的网站时,我想向他们展示弹出消息。我尝试使用window.onbeforeunload函数,但它无效。

<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit(){
     return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>

嗨Jonathan我想在弹出窗口中显示自定义消息。 目前显示默认消息 - 此页面要求您确认是否要离开 - 您输入的数据可能无法保存

2 个答案:

答案 0 :(得分:-1)

试试这个。

 $(window).bind('beforeunload', function() {        
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
            }); 

答案 1 :(得分:-2)

你核心的第二行是错误的

应该是:

window.onbeforeunload=confirmExit();

您的行缺少括号()