Page MenuHomePhorge

Show confirmation dialog also when closing tab if content changed
Open, WishlistPublic

Description

This is a bit like T15034 but related to:

  • you click a link by mistake
  • you close the tab by mistake
  • you close the browser by mistake

In short, maybe there is a way to listen to some events, like these probably:

  • onblur

This is not related just to Maniphest, but surely Maniphest is the core component involved in this, since it's easy to lose your work on a Task description if you click on a nearby link by mistake.

Also, note that the back button of your browser may not be able to recover your Task description, most of the times (or maybe just lising it all the times).

Minimal cross-browser JavaScript

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event

https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent

function areThereUnsavedChanges() {
  return true; // override me
}

/*
 * Ask for confirmation when you are closing your tab
 *
 * https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
 */
function onBeforeUnload(e) {
  if (areThereUnsavedChanges()) {
    // For security reasons, only a generic string not under the control
    // of the webpage is shown instead of this returned string
    // so you can put here whatever you want
    // https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent
    var confirmationMessage = "\\o/";

    // Gecko + IE
    (e || window.event).returnValue = confirmationMessage;

    // Safari, Chrome, and other WebKit-derived browsers
    return confirmationMessage;
  }
}
window.addEventListener('beforeunload', onBeforeUnload);

Bonus points - we should also:

  • don't register multiple similar listeners
  • activate this listener only when there is at least one change
  • remove this listener if the thing is then saved (important only if it's an Ajax action)

Event Timeline

valerio.bozzolan created this task.
valerio.bozzolan created this object in space S1 Public.

Should the confirmation dialog appear from the website or the beowser?

For this specific case, I think it's only possible to use the native confirm() stuff to break the onblur beforeunload event. I don't 100% bet on this but I'm quite sure.

Hi @Cigaryno I updated the Task description to attach a proof of concept that maybe can be interesting to expand your already-very-useful confirmation dialog

Hi @dcog thank you again for the confirmation Dialog. It saves my ass so much times.

Could the above script be interesting for you?