ASP.NET: Warning on changed data closing windows

后端 未结 6 1513
深忆病人
深忆病人 2021-01-06 22:26

I\'d like to warn users when they try to close a browser window if they didn\'t save the changes they made in the web form.

I\'m us

6条回答
  •  Happy的楠姐
    2021-01-06 22:47

    Simply add the window.onbeforeunload event. This shows up a modal window where you can decide to stay or leave the page.

    window.onbeforeunload = function (event) {
      var message = 'All changes will get lost!';
      if (typeof event == 'undefined') {
        event = window.event;
      }
      if (event) {
        event.returnValue = message;
      }
      return message;
    }
    

    All browsers will provide a standard message ("Are you sure you want to navigate away from this page?") plus your custom message.

    So the window will look like this:

    ---------------------------------
    | Are you sure you want to      |
    | navigate away from this page? |
    |                               |
    | All changes will get lost!    |
    |                               |
    |     [Cancel]     [Ok]         |
    ---------------------------------
    

提交回复
热议问题