title: Best way to execute Javascript on an anchor
body: <p>Generally, there are 3 ways (that I am aware of) to execute javascript from an <code>&lt;a/&gt;</code> tag:</p>
  
  <p>1) Use <code>onclick()</code>:</p>
  
  <pre><code>&lt;a href="#" onclick="alert('hello'); return false"&gt;hello&lt;/a&gt;
  </code></pre>
  
  <p>2) Directly link:</p>
  
  <pre><code>&lt;a href="javascript:alert('hello')"&gt;hello&lt;/a&gt;
  </code></pre>
  
  <p>3) Or attach externally:</p>
  
  <pre><code>// In an onload event or similar
  document.getElementById('hello').onclick = window.alert('Hello'); 
  return false;
  &lt;a id="hello" href="#"&gt;hello&lt;/a&gt;
  </code></pre>
  
  <p>I am actually loading the link via AJAX, so #3 is basically out. So, is it better to do #1 or #2 or something completely different? Also, why? What are the pitfalls that I should be aware of?</p>
  
  <p>Also of note, the anchor really doesn't link anywhere, hence the <code>href="#"</code>, I am using <code>a</code> so the styles conform as this is still an object to be clicked and a button is inappropriate in the context.</p>
  
  <p>Thanks</p>