简体   繁体   中英

making a custom Confirm box in javascript

I want to make a custom made confirmation box in javascipt just like the built in confirm box. the built in confirm box does not allow the code to progress unless the user selects atleast one thing. Below is my code: * ** * * HTML start * ** * *

<div class = "popUp confirm" style="z-index:40000;"  id="confirmBlock">         
<div id = "confirmLabel" >Confirm Message</div>
<div style ="border:0px solid red;height:44.56px;">
<input id="Confirm" type="button" value="Confirm" onclick = "confirmAction(1)" />
<input id = "CancelConfirm" type="button" value="Cancel" onclick = "confirmAction(0)" />
</div>
</div>

** * ** HTML end * ** * *

** * ** Javascript start * ** * *

var confirmresult = "-1";

function confirmationLoop()
{
    alert("If this alert is preesnt it works, seems like the built in alert provides some sort of pause for other parts of code to continue to work");

    if(confirmresult == "-1")
        confirmationLoop();     

    return; 
} 

function confirmAction(val)
{


    confirmresult = val;    
}

function checkuuu()
{
    confirmresult = "1";
}

function confirmMessage(message)                
{   
    document.getElementById("confirmLabel").innerHTML= message;
    //var check = setTimeout(function(){confirmAction(1)},5000);
    confirmationLoop(); 
    /*
    while(1)            //using while almost does not allow any other part to run at all hence tried recursion
    {
        if(confirmresult != "-1")
            break;  
    }
    */
    document.getElementById("confirmLabel").innerHTML= "Confirm Message";   
    var returnVal = confirmresult;
    confirmresult = -1;
    return returnVal;
}

** * ** Javascript end * ** * *

** * ** Sample code start * ** * * So this i what i expect below:

function example
{
    var check = confirmMessage(message);
    //the next part of code should not execute untill i press confirm or cancel, using settimeout or settimeinterval is asynchronous and the code flow continues. i want the effect something like alert and confirm built in boxes         
}

** * ** Sample code end * ** * *

I used loop but it keeps the thread completely occupied and does not give me a chance to press any button, which was quite obvious However recursion gives u the freedom to perform other activities. The problem even though the value of confirmResult will become 1 upon pressing confirm button, which i check through alert. the recursive loop ie confirmation loop does not seem read it as 1. it still continues as -1. If i put a alert in that confirmation loop the value wil be read as 1. Can anyone help me to achieve what i started out to?????? Ps=> sorry for such a huge question!!!

You can't use any sort of loop - as you've found it'll just cause the browser to lock up.

What you need to do is to emulate a "modal" dialog box.

This is usually done by having your dialog box appear on top of another "overlay" element which importantly covers every other element, and prevents any user interaction with them.

It's also pretty hard to implement a confirm function that'll return a value - the window.confirm method can only do that because it's synchronous - it blocks all other JS processing while the dialog is displayed.

The easiest approach is to instead supply a callback function that'll get called once the user has selected the desired value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM