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

javascript - Redirect only after confirmation - Stack Overflow

programmeradmin1浏览0评论

I want to have the user click on a link then have a javascript pop-up with a yes or no choice,but if yes redirect to different url if no, then stay on the same page. I tried the code below but it takes me to the same page when selecting yes or no.

function YNconfirm() { 
 if (window.confirm('Really go to another page?'))
{
    alert("You agree") 
window.location.href = (http:///mediclaim_portal/logout2.php');
}
else
{
window.location.href = (this.document);
}};
</script>

then 
<a href="\mediclaim_portal/logout2.php" onclick="YNconfirm()">Home</a>

I want to have the user click on a link then have a javascript pop-up with a yes or no choice,but if yes redirect to different url if no, then stay on the same page. I tried the code below but it takes me to the same page when selecting yes or no.

function YNconfirm() { 
 if (window.confirm('Really go to another page?'))
{
    alert("You agree") 
window.location.href = (http:///mediclaim_portal/logout2.php');
}
else
{
window.location.href = (this.document);
}};
</script>

then 
<a href="\mediclaim_portal/logout2.php" onclick="YNconfirm()">Home</a>
Share Improve this question edited Sep 17, 2013 at 14:47 Paul Roub 36.4k27 gold badges85 silver badges94 bronze badges asked Sep 17, 2013 at 14:17 MarkMark 311 gold badge2 silver badges5 bronze badges 2
  • stackoverflow.com/questions/7080269/… – Elen Commented Sep 17, 2013 at 14:20
  • You seem to have forgotten the ' before the url in the first part of your if block. And I think you only need http:// not http:///. Also, returning false as stated by the answers below will help. – jonhopkins Commented Sep 17, 2013 at 14:21
Add a comment  | 

9 Answers 9

Reset to default 8
function YNconfirm() { 
 if (window.confirm('Really go to another page?'))
 {
   alert("You agree") 
   window.location.href = 'http:///mediclaim_portal/logout2.php';
 }
}

then

<a href="/mediclaim_portal/logout2.php" onclick="YNconfirm(); return false;">Home</a>

The browser is navigating to the link after running your click handler.

You should change your handler to simply return false to cancel the navigation, and not set location at all.

If the code running at the onclick event doesn't return false, the href at the happens. i.e., try something like this:

function YNconfirm() { 
    if (window.confirm('Really go to another page?'))
    {
        alert("You agree");
        return true;
    }
    else
        return false;
};
</script>
<a href="\mediclaim_portal/logout2.php" onclick="return(YNconfirm());">Home</a>

Just do that ;)

<script type="text/javascript">   
function YNconfirm() { 
     if (window.confirm('Really go to another page?')){
         alert("You agree") 
         //REDIRECT
         window.location.href = (http://mediclaim_portal/logout2.php');
     }
     else{
        //DO NOTHING AND STAY IN THE SAME PAGE
        //OR SOMETHING ELSE THAT YOU WANT

        return false;
     }
};
</script>
<!-- IMPORTANT, dont link your 'a', or it will always be submited -->
<a href="javascript:void(0);" onclick="YNconfirm();">Home</a>
## Try this for all url ##

<a href="https://code.jquery.com">Click Here</a>     
       <script type="text/javascript">
          $(document).ready(function(){
            $('a').click(function(){
              //get a href url
              var url = $(this).attr('href');
              //check condition for redirection
              var answer = confirm("Leave From this page");
              if (answer){
                document.location.href = url;
              }else{
               alert("Thanks for sticking around!");
               return false;
              }
            });
          });
        </script>

Actually window.confirm returns a boolean based in our selection.

This might help: How to display a confirmation dialog when clicking an <a> link?

We can simply do:

<a href="delete.php?id=22" onclick="return confirm('Are you sure?')">Link</a>
<a href="http://mediclaim_portal/logout2.php" onclick="YNconfirm(this, event)">Home</a>

Then write the jquery script as follow

<script>
 function YNconfirm(el,ev){
    var confirm = window.confirm("Really go to another page?");
    if(confirm){
        window.location.href = $(el).attr("href");
    }else{
        ev.preventDefault();
    }
 }
</script>

I know this is not codegolf, but this is the solution I usually employ (which is slightly more readable):

<a onclick="if(confirm("Want to go to the new page?)){location.href = "bla.newpage.bla"}}

While the 'onclick return true' works, it requires internal knowledge on how onclick and href tags interact. I find my way easier on the eyes (specially if you are constantly jumping between C#, JS, SQL, etc)

The best way is to use return value of function like this

function YConfirm(
if (window.confirm('Really go to another page?')){
     return true;
 }
 return false;
)

Now simply call this function like this.

<a href="http://mediclaim_portal/logout2.php" onclick="return YConfirm();">Home</a>

Now you don't need to redirect the page with javascript.

发布评论

评论列表(0)

  1. 暂无评论