除非单击“保存”或“提交”按钮,否则onbeforeunload

时间:2014-10-15 20:07:52

标签: javascript jquery

除非我点击提交或保存按钮,否则我希望显示onbeforeunload弹出式窗口。有任何想法吗?

到目前为止,我有这个:

Drupal.behaviors.PreventNavigation = function() {
  window.onbeforeunload = function ()   {
    return "Please save your work before navigating from this page";
  }
}

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

Drupal.behaviors.PreventNavigation = function() {
  var clicked = false;
  $('#edit-save').click(function() {
    clicked = true;
  });
  $('#edit-submit-application').click(function() {
    clicked = true;
  });
  window.onbeforeunload = confirmExit;
  function confirmExit() {
   if (!clicked) {
     return "Please Save your work";
   }
  }
}