5 ways to execute a link using javascript

1: <a onclick=”test()”>Link 1</a>
2: <a href=”#” onclick=”test()”>Link 2</a>
3: <a href=”javascript:test()”>Link 3</a>
4: <a href=”javascript:void(0)” onclick=”test()”>Link 4</a>
5: <a href=”#” onclick=”test(); return false;”>Link 5</a>

1 usually won’t change the mouse cursor to a “hand cursor”, so maybe it is not as desirable in some cases.

2 seems to cause the page to shift to a new location sometimes on IE 6 and IE 7. (to top of page?)

3 … should work… but is it an old school way and should be avoided for modern browsers?

4 should work well…
the author seems to suggest it may break sometimes and try never to use href=”javascript:[anything]”

5 may work the best? according to the article above, that may be the best way as it doesn’t use href=”javascript:[something]” and the return false part will cause the href=”#” not to be evaluted.

method 5 the best way. Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s