开发者

Confirm box before closing a tab

开发者 https://www.devze.com 2022-12-22 21:09 出处:网络
I want to have a confirm box when user tries to close the window. window.onbeforeunload = function (evt) {

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 c开发者_JS百科ode.


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;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号