Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ConfirmEditNotSaved

Summary: Ask users to confirm if they are leaving the edit page without saving
Version: All
Prerequisites: Javascript enabled
Status:
Maintainer:
Categories:Editing

Questions answered by this recipe

How can I prevent editors from accidentally losing their edits?

This recipe adds Java Script code asking users to confirm if they are leaving the edit page without saving

Description

Add the following code to your Local/Config.php file. Make sure there are no unneeded line breaks.

if ($action == 'edit') {

XLSDV('en', array(
  'e_sure' => 'You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?'
  ));
XLSDV('nl', array(
  'e_sure' => 'Je doet een poging deze pagina te verlaten, weet je dat zeker? De
 wijzigingen zijn nog niet opgeslagen!!'
  ));
SDV($InputTags['e_saveeditbutton'][':html'],"<input type='submit' \$InputFormArgs onclick='needToConfirm = false;'>");
SDV($InputTags['e_previewbutton'][':html'],"<input type='submit' \$InputFormArgs onclick='needToConfirm = false;'>");
SDV($InputTags['e_cancelbutton'][':html'],"<input type='submit' \$InputFormArgs onclick='needToConfirm = false;'>");
SDV($InputTags['e_resetbutton'][':html'],"<input type='submit' \$InputFormArgs onclick='needToConfirm = false;'>");
SDV($InputTags['e_savebutton'][':html'],"<input type='submit' \$InputFormArgs onclick='needToConfirm = false;'>
<script language='JavaScript'>
  var needToConfirm = true;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm) return \"" . XL('e_sure') . "\";
  }
</script>
"
  );
}

Notes

Comments

XenoPhage March 30, 2007, at 02:07 PM

This is nice, but it causes a popup even if no edits have actually taken place. It would be far nicer to detect if any text has been changed in the textarea and then alert if the user navigates away. This can be done by using an onChange in the textarea tag.

Woody May 22, 2007, at 04:00 PM

Here is a fix, so it will only warn you when text has been changed:
Below this:
if ($action == 'edit') {
add the following lines:
SDV($InputTags['e_textarea'][':html'], 
    "<textarea \$InputFormArgs onkeydown='if (event.keyCode==27) event.returnValue=false;' ".
    "onchange='needToConfirm = true;'>".
    "\$EditText</textarea>"
    );
And comment out this line:
var needToConfirm = true;

Dale Jun 01, 2007, at 04:11 PM

Thanx for the fix. But if I comment out the variable deklaration and definition, the script does not still work. I set it to false
var needToConfirm = false;

Now every thing is running well.

merc Jun 25, 2007, at 00:06 PM

Unfortunately still doesn't work well! The Message pop ups every time I click on any GUI button in the edit window. How can I avoid it?
To fix this, you can edit script/guiedit.php and change the section:
if (preg_match('/^(.*\\.(gif|jpg|png))("([^"]+)")?$/', $tag, $m)) {
  $title = (@$m[4] > ) ? "title='{$m[4]}'" : ;
  $tag = "<img src='{$m[1]}' $title style='border:0px' onclick='needToConfirm = false;' />";
}

See Also

PITS:00639

Contributors

BrBrBr

Edit - History - Print - Recent Changes - Search
Page last modified on November 23, 2007, at 09:29 PM