Thread: Resolved javascript:void(0) confusion
View Single Post
Old 03-21-2012, 12:15 PM   PM User | #10
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by skippyV View Post
newbie here. Looking at examples I always see action commands within the anchor tag when javascript:void(0) is involved. e.g.
Code:
<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>
So it's apparent that the javascript gets executed from the 'onclick' action. But I sometimes see the same tag with NO obvious action to take - even though somehow script gets executed. And I don't know how. For example, http://technet.microsoft.com/en-us/s...ive?y=2012&m=2 has links on the right hand side of the page for the years 2012 through 2004. Here is one of those elements:
Code:
 <li> <a href="javascript:void(0)"> 2010 </a> </li>
Can someone explain to me how the javascript gets executed when this element (i.e. '2010') is clicked?
They are using code similar to this ...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
 <html lang="en">
 <head> 
 <style>
    
 </style>
 <script type = "text/javascript">
   
   </script>
 </head>
 <body>
<ul>
  <li> <a href="javascript:void(0)"> 2010 </a> </li>
  <li> <a href="javascript:void(0)"> 2011 </a> </li>
  <li> <a href="javascript:void(0)"> 2012 </a> </li>	
</ul>
  </body>
  <script>
  var A = document.getElementsByTagName("UL")[0].getElementsByTagName("A");
  for(var i = A.length;i--;){
	A[i].onclick = function(){alert(this.innerHTML)};
}
  </script>
</html>

Last edited by DaveyErwin; 03-21-2012 at 12:19 PM..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
skippyV (03-21-2012)