Question

Is there away that the confirm box appeared, if i clicked "ok" it will go to another page and if i clicked "cancel" it will just stay and the current page will not reload again? THANK YOU.

Was it helpful?

Solution

function reload()
{
    var r=confirm("Do you want to leave page!");
    if (r)
    {
        //write redirection code
        window.location = "http://www.yoururl.com";
    }
    else
   {
        //do nothing
    }
}

call this function when you want to confirmation from user.........

OTHER TIPS

You can use confirm() for this, which returns true on ok or false on cancel.

function myFunction(){
    if(confirm("Would you like go to other page?")){
        window.location = "http://yahoo.com";
    }else{
        alert('fine, if not want');
    }
}
myFunction();

Updated

DEMO

UPDATED2

<button onclick="return logout()" >logout</button>
<script>
function logout(){
    if(confirm("Would you like go to other page?")){
        window.location = "failed.php";
    }else{
        //do your stuff on if press cancel          
    }
}
</script>

You may try doing

<script>
function myfunction(){
    if(confirm("The confirm message")){
        youDoTheThingsHere();
        return false;
    }else{
       console.log("I cancelled the dialog!");
       return false;
    }
}
</script>

I'm not sure about your certain situation and the code that is involved when you call the confirm, but this has worked for me. The other option you may look at as a last resort is using something like a bootstrap modal to trigger a "confirm" modal. With the bootstrap modal you can then style it how you want... hope I could help!

Use "return None"

function myFunction(){
   if (confirm("Confirm to reset your content!")){
        location.reload();
   }else{
        return None;
   } 
}

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