1

When a user leaves a JSP page, I need to display a confirmation with yes no button "You have unsaved changes. Do you want to leave it without saving?". If the user presses "ok", then the user goes to the page s/he is navigating to. Otherwise, if "no" is pressed, the user stays on the page. My code is here:

    var formdata_original=false;
    jQuery(".outConfirmPlugin").click(function () {

        if (formdata_original == false) {
            con();
        }
        return formdata_original;
    });
    function con() {
        $.confirm({
            'title':'',
            'message':settings.jsMessage,
            'buttons':{
                'Yes':{
                    'class':'blue',
                    'action':function () {
                        formdata_original = true;

                    }
                },
                'No':{
                    'class':'gray',
                    'action':function () {

                    }
                }
            }
        });
    };

I know my error is: function "con" and "return formdata_original;" - they are not synchronized. How can i do this?

rationalboss
  • 5,330
  • 3
  • 30
  • 50
nukipo
  • 31
  • 3
  • possible duplicate of: http://stackoverflow.com/questions/887029/how-to-implement-confirmation-dialog-in-jquery-ui-dialog – mbx-mbx Sep 21 '12 at 13:35

1 Answers1

0

try return simple value from you function, i mean

action':function () {
                       return true;
                    }

and when you call 'con' function you will be able to write

 formdata_original = con(); 

In this case you can not worry about sinhronize

The second option is creation global object that belongs ot window or $. So try

window["formdata_original"] = false

and in your code inside confirm dialog

window["formdata_original"]=true.
Anton Baksheiev
  • 2,211
  • 2
  • 14
  • 15
  • I think, as i understood from you description you need deside submit or not from. So you need in action for YES write "$('#ID_FORM').submit()" Because in currect structure of code firstly result of click will be return false, and after that your formdata_original will be changed. – Anton Baksheiev Sep 21 '12 at 14:06
  • in addition you are able to override event submit of the form, and on the step of confirm ation submit or not form, but in this you need return false in order to prevent bubbling. – Anton Baksheiev Sep 21 '12 at 14:08
  • if it is not stricted requirment of usinf confirm plugin you can consider of usinf nativ jquery plugin http://jqueryui.com/demos/dialog/#default – Anton Baksheiev Sep 21 '12 at 14:09