Confirm box before closing a tab

谁说胖子不能爱 提交于 2019-12-18 17:32:45

问题


I want to have a confirm box when user tries to close the window.

window.onbeforeunload = function (evt) {
    var message = 'Are you sure you want to leave, cause there are some unsaved changes?';
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt) {
        evt.returnValue = message;
    }

    return message;
}

The thing is I want to check a variables value

var sncro = 1;

If its value is not equal to one then this confirmation box should be there, else no need to have a confirmation. I'm not able to figure this. Its so silly but I request anybody can have a look on the code.


回答1:


I assume that on page load, you are setting up var sncro=1; and when some data changes, you adjust this value. Here is the quick check:

window.onbeforeunload = function (evt) {
  if (sncro != 1) {
   var message = 'Are you sure you want to leave, cause there are some unsaved changes?';
   if (typeof evt == 'undefined') {
      evt = window.event;
   }
   if (evt ) {
      evt.returnValue = message;
   }
   return message;
  }
}


来源:https://stackoverflow.com/questions/2452963/confirm-box-before-closing-a-tab

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