Links vs. JavaScript Event Handlers

posted by admin in HTML, JavaScript, Uncategorized

Anchor tags are frequently used to do something other than what they were meant for, such as executing some JavaScript code.  I’m talking about cases such as these:

<a href="javascript:doSomething()">Click Here</a>

// or...
<a href="javascript:void(0)" onclick="doSomething()">Click Here</a>

Both of these are bad practice and should be avoided.  The purpose of the anchor tag is to provide a link to a separate page so that the its interpreter (human or program, such as a search crawler) can use the value of its HREF attribute to reach another page (URL).  If what you are putting into the HREF attribute is not a valid URL, you’re very likely using the wrong tag.

More