dimanche 28 octobre 2018

Dialog action before leaving a page

I have found lots of solutions but none of them really solve my problem. I have to perform certain actions on a page if the user decides to leave without saving his changes (ask him if he really wants to leave without saving, or let him save them, and some bookkeeping ). Most answers mention the onbeforeunload event.

Something like

$(window).on('beforeunload ',function() { return 'Are you sure ?'; });

But this is not exactly what I need. First, it doesn't let me do a dialog as the user requested. The ideal for me is using a jQuery.UI modal dialog or something similar, with custom messages and two clear buttons: "Leave the page without saving" and "Save and leave" Also, this only control if I could leave or not the page, but I need to do other operations (for example, there are some pages in which the user is allowed to silently save the info and leave... don't ask me, the Business Analyst decided that) So, it is possible to do something like this pseudocode?

$(window).on('some event when I leave the page ',
     function() { 
        destiny = DetectDestinyPage();
        if (destiny in list of PagesToAsk and FormHasNotSavedInfo()){
           answer = modalDialogAskingTheClientIfHeWantToSave()
           if(answer == "yes"){
               ajaxCallToServerToSave()
            }
        }
        GoToDestiny(destiny)
      }
  }




Aucun commentaire:

Enregistrer un commentaire