Reputation: 31
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>
Upvotes: 2
Views: 27626
Reputation: 442
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>
Upvotes: 1
Reputation: 445
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)
Upvotes: 0
Reputation: 11
## 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>
Upvotes: 1
Reputation: 662
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.
Upvotes: -1
Reputation: 328
<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>
Upvotes: 0
Reputation: 348
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>
Upvotes: 2
Reputation: 1924
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>
Upvotes: 2
Reputation: 36438
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>
Upvotes: 8
Reputation: 887305
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.
Upvotes: 2