How to control when closing a window

ぐ巨炮叔叔 提交于 2019-12-23 05:32:13

问题


I have this code, which alerts the user that the window is about to be closed and the user may loose the data. If the user accpets the window closes, if not, the user stays on the page.

<script language='javascript'>
   ClosingVar =true
   window.onbeforeunload = ExitCheck;
   function ExitCheck() {
      if(ClosingVar == true) { 
         ExitCheck = false;
         return "YOU MAY LOOSE YOUR DATA.";
      }
   } 
</script>

I want to be able to manage the option of the user. If the accept the closing option, then I want to call a function to do something and then close the window.

Does anyone know how to do this?

Thanks a lot.


回答1:


Try this code I made for you, based on this question.

ps: I know the question in reference uses jQuery, but it's not required.

window.onbeforeunload = function () {

    setTimeout(function () {
        setTimeout(function () {

            alert('Welcome back!');
            // this code will run if they choose to stay on the page
            // run your other code here

        }, 100);
    }, 1);

    return 'YOU MAY LOOSE YOUR DATA.';
};



回答2:


You can use the beforeunload event with Jquery.

The following link could help: link



来源:https://stackoverflow.com/questions/18068912/how-to-control-when-closing-a-window

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