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
|
9 Answers
Reset to default 8function 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.
'
before the url in the first part of your if block. And I think you only needhttp://
nothttp:///
. Also, returning false as stated by the answers below will help. – jonhopkins Commented Sep 17, 2013 at 14:21