最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript confirm box, if cancel is clicked, it will not reload the page - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Mar 1, 2014 at 13:16 YanyanYanyan 1372 gold badges2 silver badges7 bronze badges 6
  • 1 Sure, there are a lot of ways – Johan Commented Mar 1, 2014 at 13:18
  • 1 if ( confirm("RELOAD?") ) location.reload(); – Buch Commented Mar 1, 2014 at 13:19
  • I mean it will not reload if you clicked cancel. @Buch – Yanyan Commented Mar 1, 2014 at 13:21
  • Can you teach me one way how? @Johan – Yanyan Commented Mar 1, 2014 at 13:22
  • 1 it does not - if you click cancel, confirm returns false, and location.reload() doesn't execute – Buch Commented Mar 1, 2014 at 13:22
 |  Show 1 more ment

4 Answers 4

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

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

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.";
    }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;
   } 
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论