We help IT Professionals succeed at work.

alert href

vidhubala
vidhubala asked
on
Medium Priority
2,187 Views
Last Modified: 2012-06-27
i want the href value to alert when i click.
 can we simplify the above code?
 for eg can i remove the onmouseover function? can i remove few lines in the function?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
	<script>
		function locatehref(arraynum)
		{
			var arraynum;
			var whathref;
			whathref = document.links[arraynum].href;
			alert(whathref);
		}
  </script>
 </HEAD>
 <BODY>
	<a href="yourlink.com" onmouseover="locatehref(0)">test1</a>
	<a href="mylink.com" onmouseover="locatehref(1)">test1</a>
	<a href="theirlink.com" onmouseover="locatehref(2)">test1</a> 
 </BODY>
</HTML>

Open in new window

Comment
Watch Question

Commented:
This should work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
        <script>
                function locatehref(link)
                {
                        alert(link.href);
				//return false so that you want go to the link
				return false;
                }
  </script>
 </HEAD>
 <BODY>
        <a href="yourlink.com" onclick="return locatehref(this)">test1</a>
        <a href="mylink.com" onclick="return locatehref(this)">test1</a>
        <a href="theirlink.com" onclick="return locatehref(this)">test1</a> 
 </BODY>
</HTML>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
Well, actually you don't need a function at all...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
   <title>A document has to have a title to be valid.</title>
</head>
<body>
   <a href="yourlink.com" onclick="alert(this.href);return false;">test1</a>
   <a href="mylink.com" onclick="alert(this.href);return false;">test1</a>
   <a href="theirlink.com" onclick="alert(this.href);return false;">test1</a> 
</body>
</html>

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.