Generally, there are 3 ways (that I am aware of) to execute javascript from an <a/>
tag:
1) Use onclick()
:
<a href=# onclick=alert('hello'); return false>hello</a>
2) Directly link:
<a href=javascript:alert('hello')>hello</a>
3) Or attach externally:
// In an onload event or similar
document.getElementById('hello').onclick = window.alert('Hello');
return false;
<a id=hello href=#>hello</a>
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?
Also of note, the anchor really doesn't link anywhere, hence the href=#
, I am using a
so the styles conform as this is still an object to be clicked and a button is inappropriate in the context.
Thanks