Redirect only after confirmation

2020-07-24 05:22发布

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>

8条回答
仙女界的扛把子
2楼-- · 2020-07-24 05:50

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>
查看更多
迷人小祖宗
3楼-- · 2020-07-24 05:52

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>
查看更多
Fickle 薄情
4楼-- · 2020-07-24 05:54

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)

查看更多
Emotional °昔
5楼-- · 2020-07-24 06:03

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.

查看更多
Summer. ? 凉城
6楼-- · 2020-07-24 06:05
## 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>
查看更多
贼婆χ
7楼-- · 2020-07-24 06:12
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>
查看更多
登录 后发表回答