window.onbeforeunload检测POST或GET

rid*_*00z 3 javascript-events

在window.onbeforeunload事件中有没有办法检测新请求是POST(在同一页面上)还是GET(转到页面)?看到新的document.location也很棒.

window.onbeforeunload = winClose;
function winClose() {
    //Need a way to detect if it is a POST or GET
    if (needToConfirm) {       
        return "You have made changes. Are you sure you want?";
    }
}
Run Code Online (Sandbox Code Playgroud)

mar*_*son 12

这就是我刚刚做到的方式:

$(document).ready(function(){
  var action_is_post = false;
  $("form").submit(function () {
    action_is_post = true;
  });

  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (!action_is_post)
      return 'You are trying to leave this page without saving the data back to the server.';
  }
});
Run Code Online (Sandbox Code Playgroud)