Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript confirm box, if cancel is clicked, it will not reload the page

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.

like image 805
Yanyan Avatar asked Mar 01 '14 13:03

Yanyan


2 Answers

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>
like image 42
Suman Bogati Avatar answered Oct 20 '22 00:10

Suman Bogati


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.........

like image 111
Kuldeep Choudhary Avatar answered Oct 20 '22 00:10

Kuldeep Choudhary