简体   繁体   English

关闭选项卡前确认框

[英]Confirm box before closing a tab

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. 如果其值不等于1,则此确认框应该在那里,否则无需确认。 I'm not able to figure this. 我无法想象这一点。 Its so silly but I request anybody can have a look on the code. 它是如此愚蠢,但我要求任何人都可以查看代码。

I assume that on page load, you are setting up var sncro=1; 我假设在页面加载时,你正在设置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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM