2

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mark
  • 31
  • 1
  • 2
  • 5
  • http://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page – Elen Sep 17 '13 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 Sep 17 '13 at 14:21

9 Answers9

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

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>
Assaf Hershko
  • 1,846
  • 4
  • 18
  • 20
2

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>
wiLLiamcastrO
  • 238
  • 3
  • 13
1
## 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>
1

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>
Rizwan Amjad
  • 329
  • 4
  • 11
0
<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>
Zeeshan Cornelius
  • 308
  • 1
  • 4
  • 15
0

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)

CyberClaw
  • 435
  • 3
  • 13
-1

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.

Davinder Kumar
  • 652
  • 4
  • 17