在浏览器窗口/选项卡上打开自定义弹出窗口关闭

问题描述:

我正在尝试在浏览器窗口/选项卡上打开自定义弹出窗口。

I am trying to open a custom popup on browser window/tab close.

详细说明如果用户点击浏览器窗口/选项卡关闭按钮,会出现带有某些内容的自定义弹出窗口,或者可能有某些选项要求关闭或继续该页面。

In details if a user clicks on the browser window/tab close button a custom popup will appear with some content or may be having some option asking to close or continue the page.

这里是只带来默认警报弹出窗口的代码:

here is the code which only bring the default alert popup:

window.onbeforeunload = function() {
              var message = 'Do you want to leave this page?';
              return message;
          }


我也建议你不要这样做,但毕竟,你问了一个问题并得到答案。

i would also suggest you dont do this, but after all, you asked a question and deserve an answer.

<script type="text/javascript">
     var popit = true;
     window.onbeforeunload = function() { 
          if(popit == true) {
               popit = false;
               return "Are you sure you want to leave?"; 
          }
     }
</script>

此脚本可以使用( http://jsfiddle.net/SQAmG/3/
并确保它只会弹出一次(如果他决定不再打扰用户)留一次)。

this script will work (http://jsfiddle.net/SQAmG/3/) and will make sure that it will only popup once (to not bother the user again if he decided to stay once).