What is the best crossbrowser way to make anchor without href (javascript-driven) behave like real anchor? The most obvious one is to use # as anchor but it makes page jump...
These will not jump:
<a href="#null">Click</a>
<a href="#n">Click</a>
<a href="#undefined">Click</a>
They are, however, soul destroying and extremely ugly.
A do-nothing link, that doesn't even jump:
<a href="javascript:void(0)">link</a>
Update: As linked document suggests (pointed out by Ambrosia), since void(0)
returns undefined
, it is better to actually write above code as:
<a href="javascript:undefined">link</a>
Unless, of course, undefined has been re-defined.
Maybe this is OK too:
<a href="javascript:;">link</a>
If you're reacting to the click event, just make sure you return false and whatever is in the href, whether a #hash or other url, will be ignored.
<a href="#anything" onclick="doSomething(); return false;">link</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With