Trigger “Changes you made may not be saved” message in WordPress edit post

If you add custom content in admin using ajax you probably want to trigger an alert when user wants to reload page or navigate on other page.

This is an event  beforeunload  bind to  $(window)

If you use plain  beforeunload you will have issues on WordPress because in edit post WordPress uses   beforeunload.edit-post and when you press update or publish it will not unbind that event. To make it work use the code below.

Add this to your trigger in jQuery: