-
Very simply maybe
Hi!
I am unable to detemine how to do the following:
I have a basic html <ahref link:
Code:
<a href="test.php?id=1">Test</a>
Now when user clicks on the above links it must be confired weather he wants to go at it or not...i mean just a yes, no box, if user presses yes then he's taken to that link else just stay in there.
How can i achieve thus using javascript ?
Thanks!
-
Re: Very simply maybe
Something to the extend of:
Code:
function takeme(url){
if (confirm("Are you sure?")) {
location.href=url;
} else
{
//clicked cancel - do nothing
}
}
the something like
Code:
<a href="javascript:takeme('http://google.com');">click</a>
On a sidenote.. I think if the user clicks on a link he's pretty sure he wants to go there so this script seems abit useless to me, sorry.
-
Re: Very simply maybe
No! Bad Jop! ;)
This solution is inaccessible. It's better to do:
<a href="target.html" onclick="return confirm("Really?")">Hit Me!</a>
This way, users without JavaScript can use the link. (They don't get the confirmation though.)
Of course, it would be even better not to put the script inline, but attach it from an external JS file.
-
Re: Very simply maybe
A better way would be to use the onclick event. Its also easier and means the link will still work even if Javascript is disabled:
Code:
<a onclick="return confirm('Are you sure?')" href="test.php?id=1">Test</a>
-
Re: Very simply maybe
Hi!
Thanks for the help. Can i have yes/no buttons as Proceed/Cancel ?
Thanks!
-
Re: Very simply maybe
No. Some browsers will use Yes/No, other OK/Cancel, but you have no control over it.
-
Re: Very simply maybe
Oi oi thanks for correcting me CornedBee I should've known better!
To make up for my mistake, the unobtrusive version:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>Link confirmation</title>
<script type="text/javascript">
function confirmLink(url){
if(confirm("Are you sure?"))
location.href=this.getAttribute("href");
else
return false;
}
var addOnclickHandlers = function(){
var links = document.getElementsByTagName("a");
for(var x=0;x<links.length;x++){
if(links[x].className == "confirm")
links[x].onclick = confirmLink;
}
}
window.onload = addOnclickHandlers;
</script>
</head>
<body>
<a href="http://google.com" class="confirm">google</a>
</body>
</html>
-
Re: Very simply maybe
Hi!
Thanks for all the suggestions and comments. I used visualAd's code which is working cool! :)
Thanks!