Summary: Warn authors when they move away from a page without saving it; optionally request an edit summary or an author name
Version: 20170712
Status: beta
Maintainer: Petko
Categories: Editing, PHP55, PHP72
Users: +3 (view / edit)
License: GPL3+

Note: A feature based on this recipe will be included in PmWiki 2.2.119, and can be enabled in config.php with:
$EnableNotSavedWarning = 1;
If you enable the core feature, please disable the current recipe.


Warn authors when they move away from a page without saving it.

(Dialog boxes and text differ in different browsers.)

It happens that authors modify a page, but instead of pressing the Save button, they click on a link that leads away from the edit form. In that case, all their changes are lost.

This recipe will display a confirmation dialog box, and the user can press "Cancel" or "Stay on page" to return to the edit form.

Additionally, when previewing a page, the recipe will make the text area scroll to the previous position, which is more convenient for long pages. Note; the recipe RememberEditPosition has a similar feature, but requires an additional input element, which is not required with the current recipe.

Optionally, the recipe can be configured to request a user name if the "Author" field is left empty, and an edit summary if the "Summary" field is empty, see below for configuration.

Demo - test the recipe here.


  • Copy notsavedwarning.phpΔ to your /cookbook/ directory (alt. download: notsavedwarning.php).
  • Add to config.php this code:
      $NsPromptSum    = XL("Please enter a summary"); # Optional, read below.
      $NsPromptAuthor = XL("Please enter your name"); # Optional, read below.

The lines $NsPromptSum = "Caption"; and $NsPromptAuthor = "Caption"; are not required. Each will cause a "prompt" box to appear if the form is submitted with an empty "Summary" or "Author" field.

It just reminds to enter a summary (or an author), without enforcing one -- if the user fills nothing, the form is still submitted. See $EnablePostAuthorRequired if you need to enforce an author name.

Other config.php variable (before the include_once line):

  # disable the warning if a user leaves the edit form
  $NsWarning = false;

  # custom warning message (note: some browsers ignore a custom message and
  #       display a default preset message about unsaved text in the page)
  $NsWarning = XL("Abandon changes and exit?");


  • The dialog box is done in JavaScript, so the authors need to have recent browsers with JavaScript enabled.
  • Browsers which do not recognize the OnBeforeUnload Event, may not work. (Mozilla, MSIE and Chrome work, Konqueror and Opera don't seem to.)
  • The confirm box will only show if the content in the edit form is actually modified. It works with all text fields, so the Edit Attributes recipe works fine, and possibly other recipes enhancing the edit form.
  • In "Preview" mode, after the Preview appears, the recipe assumes that the content was modified before pressing Preview so the warning will appear if the user tries to leave the page without saving it.

Release Notes

  • 20170712 : Fix bug when a "Cancel" button is pressed, reported by Daniel Palmer.
  • 20150828 : Slight optimization.
  • 20150815 : Store the position of the textarea scroll when preview (PITS:01019, inspired by RememberEditPosition but implemented differently).
  • 20150331 : Fix for RecipeInfo compatibility.
  • 20150329 : Modified the warning to work with the CodeMirror recipe.
  • 20110829 : It should now work with the MiniEdit recipe.
  • 20110616 : Adaptation for an alternative edit form.
  • 20090612 : Fix: MSIE versions 7 and 8 block the "prompt" box where the user types their name/summary (reported by David Pilling). To those users, now the recipe shows simple "alert" box with the message and blocks the form post.
  • 20090428b : Fix: removed warning on pressed Cancel.
  • 20090428 : Add: $NsWarning; Fix: the form is not submitted if the user pressed Cancel on the prompt box; Fix: no prompt box on preview or cancel button.
  • 20090425d : Fix captions to allow apostrophes.
  • 20090425c : Changed the install format to allow the setting of captions.
  • 20090425b : Add $NsPromptAuthor.
  • 20090425 : Add $NsPromptSum - optional prompt box to enter a change summary.
  • 20090311 : Fix for Preview mode.
  • 20090307 : First public release, ready to be tested.

See Also

Cookbook /
ConfirmEditNotSaved  Ask users to confirm if they are leaving the edit page without saving
RememberEditPosition  Return the editing textarea to the previous position when previewing a page (beta)
RequireAuthor  Require author names when saving pages (Documentation)
RequireSummary  Require a summary to be entered
00994  page edits are LOST when 'Attach' is selected (Closed (3 Cookbook recipes))
01019  Store the position of scrollbar of textarea when preview (Closed)


  • The recipe is written and maintained by Petko (5ko [snail] 5ko [period] fr).
  • If this recipe helps you or saves you time, you can help support its continued development by .


Your comments are welcome.

User notes +3: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.