1

私の実際のタスクは、ユーザーがタブまたはウィンドウを閉じたときにユーザーを確認することです。そのために、次のコードを書きました。

 var validNavigation = false;

 function wireUpEvents() {

  var dont_confirm_leave = 0; 
   var leave_message = 'You sure you want to leave?'
  function goodbye(e) {
    if (!validNavigation) {
      if (dont_confirm_leave!==1) {
         if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = leave_message;
       //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
        //return works for Chrome and Safari
        return leave_message;
      }
    }
  }
  window.onbeforeunload=goodbye;

  // Attach the event keypress to exclude the F5 refresh
  $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type='button']").bind("click", function() {
    validNavigation = true;
  });

}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
  wireUpEvents();
});

ここでの問題は、ユーザーが Web ページをリロードした場合に確認を求めることです。したがって、リロード イベントをバインドする必要があります。

助けてください...よろしくお願いします

4

1 に答える 1

1

.unloadまたはを使用.on('unload', handler):

$(window).unload(function() {
  // your code here
});
于 2013-09-11T11:08:17.643 に答える