Results 1 to 5 of 5

Thread: achive anchors

  1. #1
    Join Date
    Feb 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default achive anchors

    i have a page and there are anchors in page and if user click anyone i want to learn which ancor clicked

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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>

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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?

  5. #5
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •