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>
Just do that ;)
If the code running at the onclick event doesn't return false, the href at the happens. i.e., try something like this:
I know this is not codegolf, but this is the solution I usually employ (which is slightly more readable):
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 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 setlocation
at all.then