onbeforeunload在其他情况下根本不起作用

时间:2018-10-11 05:32:54

标签: javascript

我正在尝试设置if-else条件,如果if部分按预期运行,但是我不知道为什么else部分根本没有运行..这是我的代码:

window.onload = function (e) {
let page = location.pathname.split('/')
if (page[1] === "timesheets") {

  window.onbeforeunload = function(e){
    var flag = true;
    if(!!e.explicitOriginalTarget ){
      if(!!e.explicitOriginalTarget.classList){
        flag = !e.explicitOriginalTarget.classList.contains("noprompt");
      }
    }
    if(flag){
      $.ajax({
        url: "/timesheets/stop_running_timesheet",
        type: "GET",
        dataType: "json",
        success: function(data){
          var flag = false;
          if(data.timesheet){
            flag = confirm("Press OK to stop Timesheet# " + data.timesheet.timesheet_id + " and continue");
          }
          if(flag){
            $('body').find('tr[id="timesheet-'+data.timesheet.id+'"]').trigger('click');
            $('.edit-timesheet-button').trigger('click')
            updateRunningTimesheet(data.timesheet);
          }
        }
      });
      return 'Are you sure you want to leave?';
    }
  };
} else {
  console.log("Updating timesheet.......")
  window.onbeforeunload = function(e){
    var flag = true;
    if(!!e.explicitOriginalTarget ){
      if(!!e.explicitOriginalTarget.classList){
        flag = !e.explicitOriginalTarget.classList.contains("noprompt");
      }
    }
    if(flag){
      $.ajax({
        url: "/timesheets/save_timesheet_from_different_page",
        type: "GET",
        dataType: "json",
        success: function(data){
          var flag = false;
          if(data.timesheet){
            flag = confirm("Press OK to stop Timesheet# " + data.timesheet.timesheet_id + " and continue");
          }
          if(flag){
            $('body').find('tr[id="timesheet-'+data.timesheet.id+'"]').trigger('click');
            $('.edit-timesheet-button').trigger('click')
            updateRunningTimesheet(data.timesheet);
          }
        }
      });
      return 'Are you sure you want to leave?';
    }
  }
}
};

0 个答案:

没有答案