PDA

View Full Version : achive anchors



sir_yunus
03-09-2008, 02:58 PM
i have a page and there are anchors in page and if user click anyone i want to learn which ancor clicked

codeexploiter
03-10-2008, 06:07 AM
Check the following source


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">

</style>
<script type="text/javascript">
var determineTheClickedLink = function(lnk){
alert(lnk.href);
alert(lnk.innerHTML);
}
</script>
</head>
<body>
<a href="http://www.google.com/" onclick="determineTheClickedLink(this);">Test</a>
</body>
</html>

tech_support
03-10-2008, 06:29 AM
Or, even better:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Links Finder</title>
<script type="text/javascript">
function getLinks() {
var e = document.getElementsByTagName('a');
for (var i=0; i < e.length; i++) {
e[i].onclick=function() {
//link: this.href
//link name: this.innerHTML
alert('You\'re going to '+this.href);
//If you DON'T want to go to the link, then uncomment the next line:
//return false;
}
}
}
window.onload=getLinks;
</script>
</head>

<body>
<p><a href="http://google.com/">Google</a></p>
<p><a href="http://yahoo.com/">Yahoo</a></p>
</body>
</html>

codeexploiter
03-10-2008, 06:40 AM
Hi Tech_support,

The problem in the latest script is it will convert all the links in this manner. Consider in this page later the user wants to add his own some other click event then how could that be accommodated?

tech_support
03-10-2008, 07:33 AM
Just be a bit more creative:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Links Finder</title>
<script type="text/javascript">
function getLinks() {
var e = document.getElementsByTagName('a');
for (var i=0; i < e.length; i++) {
if (e[i].rel=='grab') {
e[i].onclick=function() {
//link: this.href
//link name: this.innerHTML
alert('You\'re going to '+this.href);
//If you DON'T want to go to the link, then uncomment the next line:
//return false;
}
}
}
}
window.onload=getLinks;
</script>
</head>

<body>
<p><a href="http://google.com/" rel="grab">Google</a></p>
<p><a href="http://yahoo.com/" rel="grab">Yahoo</a></p>
<p><a href="http://example.com/">Look! this link doesn't do anything!</a></p>
</body>
</html>