Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor without href

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...

like image 433
SiberianGuy Avatar asked May 22 '10 00:05

SiberianGuy


4 Answers

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.

like image 92
karim79 Avatar answered Sep 25 '22 09:09

karim79


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.

like image 26
mr.b Avatar answered Sep 23 '22 09:09

mr.b


Maybe this is OK too:

<a href="javascript:;">link</a>
like image 25
Mr. Míng Avatar answered Sep 21 '22 09:09

Mr. Míng


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>
like image 35
great_llama Avatar answered Sep 24 '22 09:09

great_llama