Why Href="#" Is High Priority Than Onclick
Solution 1:
It isn't higher priority. The onclick fires and then the browser follows the link.
If you don't return false
(note spelling) or func
throws an error (thus not reaching the return
statement) the event won't be canceled.
(As fallbacks for if the JS fails or is disabled go, however, a link to the top of the page is really sucky. Progressive enhancement is the way forward.)
Solution 2:
If the only reason you want to have a href
value is to enable the hand cursor, you can use css style instead:
<astyle="cursor:pointer;"onclick="func();return false">click</a>
Solution 3:
...and a decade later, here's the answer to the question asked above;
<ahref="#"onclick="event.preventDefault(); func()">click</a>
To save confusion, this answer does not address the "return false" aspect. That subject can be researched separately.
PS; Technically there is no hierarchy (AKA "priority") for what happens when an Anchor Tag is clicked. But since HREF is the "default" action / event for an anchor tag, from a certain perspective, HREF does "take priority" over OnClick.
Post a Comment for "Why Href="#" Is High Priority Than Onclick"