Sayamima
Sayamima

Reputation: 225

Need to halt a Confirm window of javascript

I have been struck up in a condition where i need to halt the Confirm box. This is what I have done:

function onBeforeClientInsert(record) {
    var eventtype = parseInt(record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EVENT_TYPE_ID").GetFieldDataFieldId() % > );
    var begindate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("BeginDate").GetFieldDataFieldId() % > ;
    var enddate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EndDate").GetFieldDataFieldId() % > ;

    $.ajax({
        type: "POST",
        url: "Data.aspx/CheckInsertRecord",
        data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," + "EndDate:'" + enddate + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            if (msg.d == "No duplicate") {

            } else {
                alert(msg.d);
                eval("var data = " + msg.d + ";");

            }
            var i = 0;
            alert(i);
            alert(data[i]);
            do {
                $("#beginDate").html(data[i].BeginDate);
                $("#eventTypeID").html(data[i].EVENT_TYPE_ID);
                $("#endDate").html(data[i].EndDate);
                $("#beginlatlong").html(data[i].BeginLATLONG);
                $("#endlatlong").html(data[i].EndLATLONG);

                var modal = document.getElementById('Div1');
                modal.style.display = '';
                modal.style.position = 'fixed';
                modal.style.zIndex = '100';
                modal.style.left = '30%';
                modal.style.top = '10%';


                var screen = document.getElementById('modalScreen');
                screen.style.display = '';
                i++;
                if (confirm("Are you sure you want to continue?") == false) {
                    hide();
                    continue;
                }


            }

            while (msg.d != null);
        }
    });


    if (confirm("Are you sure you want to insert this new record ?") == false) {
        hide();
        return false;
    }
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
        hide();
        SetPostBackCause('INSERT');
        return true;
    }
    return false;
}

So,the problem has been that

if (confirm("Are you sure you want to insert this new record ?") == false) {
    hide();
    return false;
}

would be run immediately after the confirm box

if(confirm("Are you sure you want to continue?")==false){
    hide();
    continue;
}

But i want it to be halted until the user clicks something on the first confirm box. Can u please let me know how to do this? Also Can u let me know any other way to do this, if I'm approaching it in wrong way?

Upvotes: 0

Views: 153

Answers (3)

Michael
Michael

Reputation: 4090

Have you considered the async: false option to $.ajax?

Upvotes: 0

zsalzbank
zsalzbank

Reputation: 9857

You need to place all of this:

if (confirm("Are you sure you want to insert this new record ?") == false) {
        hide();
        return false;
    }
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
        hide();
        SetPostBackCause('INSERT');
        return true;
    }

within your AJAX callback if you want it to run after the first dialog. Currently, you set the callback, and then call the code above. If your internet connection was really slow, the first dialog you are seeing could theoretically come after the second you are seeing.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359786

Ajax is asynchronous.

You need to move all dependent code into the success callback.

success: function (msg) {
    // snip ...
    
    if (confirm("Are you sure you want to insert this new record ?") == false) {
        hide();
    }
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
        hide();
        SetPostBackCause('INSERT');
    }
}

You could use async: false to make the request sychronous but I do not recommend this.

Upvotes: 2

Related Questions