0

I have overridden the default alert box with a custom dialog box. But I want the script to be paused until the cancel button on the dialog box is clicked. How to achieve it using javascript or jquery ?

P.S. : I am trying to make a dialog box which has the same functionality as an alert box.

Sanjeev
  • 855
  • 1
  • 9
  • 15

2 Answers2

1

You can not write blocking code in javascript as it is single threaded and runs on the same thread as the UI, see here: Blocking "wait" function in javascript?

You could do it via callbacks or events that get fired when your custom alert box gets closed.

function CustomAlert(message, callback)
{
    alert(message);
    callback();
}

function CodeWhichGetsBlocked()
{
    DoSomething();
    CustomAlert("continue?", function() {
        DoSomething();
    });
}
Community
  • 1
  • 1
Gigo
  • 3,188
  • 3
  • 29
  • 40
  • If I override the alert with a dialog the callback will be executed without waiting for me to click the cancel button. I want the script to be paused until the cancel button is clicked. – Sanjeev Oct 08 '13 at 18:37
  • Yes, you have to execute the callback when the cancel button is clicked. I just gave an example. – Gigo Oct 08 '13 at 18:53
0

I will use dynamic HTML.

var $divAlert = $("<div>Are you sure going to next step?</div>");

$divAlert.dialog({
                autoOpen: true,
        height: "auto",
        width: "auto",
        modal: true,
        buttons: {
            "OK": function(){
                //Your Code/Logic goes here
            },
                    "Cencel": function(){
                        $(this).dialog("close");
                    },
        }
});

you can also customize in temrs of design your dialog just add class.

like,

dialogClass: "MyDialog"
Rickyrock
  • 328
  • 2
  • 4
  • 21
  • Boss ! I am trying to make the dialog similar to alert... i.e. The execution of the script should be paused when the dialog is appeared. – Sanjeev Oct 08 '13 at 18:41