i have a page and there are anchors in page and if user click anyone i want to learn which ancor clicked
i have a page and there are anchors in page and if user click anyone i want to learn which ancor clicked
Check the following source
Code:<?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>
Or, even better:
Code:<!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>
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
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?
Just be a bit more creative:
Code:<!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>
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
Bookmarks