Question

I am trying to write library code for alert/confirm/prompt replacements using jquery and impromptu.

I want to be able to call the functions in the same way:

eg. var getval = jConfirm("Are you sure?"); alert (getval);

My function:

function jConfirm(text, title){
var return_val = false;

$.prompt(text, {
    title: title,
    html: text,
    buttons: { OK: true, Cancel: false },

    callback:function(e,v,m,f)
    {
        return_val = v;
    }
});

return return_val;}

The code currently continues to execute without waiting for input - the alert is displayed straight away and 'getval' never gets a value.

How can I get impromptu to wait for an input from the dialog the same way the native confirm function does and return a value to getval before continuing?

Was it helpful?

Solution

You can not do that as confirm/alert/prompt are JS functions that calls the execution of code in the Browser application, which is written with a language (eg. C#, Java) that allows the creation of true modal dialogs: windows that stops the execution of the application until the user closes or interacts with it. So, in your case, the jConfirm method can't stop the execution of the page as alert can do. To do what you need i suggest to pass to the jConfirm method a callback that will be executed after the impromptu callback only if the user clicks ok

jConfirm("Are you sure?","Confirm",dialogResult);
function dialogResult(v)
{
    alert(v);
}

function jConfirm(text, title, dialogResultCallback) {  
    $.prompt(text, {
        title: title,
        html: text,
        buttons: { OK: true, Cancel: false },

        callback:function(e,v,m,f)
        {
          if(v)
           dialogResultCallback(v);
        }
    });
}

OTHER TIPS

Before you get too deep into writing your own dialog system have you seen jQuery ui? It provides a simple function to create modal dialogs. http://jqueryui.com/dialog/#modal

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top