How to show the “Are you sure you want to navigate away from this page?” when changes committed?

前端 未结 17 1774
深忆病人
深忆病人 2020-11-22 02:11

Here in stackoverflow, if you started to make changes then you attempt to navigate away from the page, a javascript confirm button shows up and asks: \"Are you sure you want

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 03:08

    When the user starts making changes to the form, a boolean flag will be set. If the user then tries to navigate away from the page, you check that flag in the window.onunload event. If the flag is set, you show the message by returning it as a string. Returning the message as a string will popup a confirmation dialog containing your message.

    If you are using ajax to commit the changes, you can set the flag to false after the changes have been committed (i.e. in the ajax success event).

提交回复
热议问题