
Ask HN: Href=“#” VS href=“JavaScript:;” - optikals
Sorry for my silly question, can you tell me if there&#x27;s a difference between href=&quot;#!&quot; and href=&quot;javascript:;&quot;<p>and which one it&#x27;s better to use on the links with JavaScript attached to them (nothing else, JS only).<p>I know it&#x27;s not the correct use, it should be a &quot;button&quot;, but I really need to use &lt;a&gt;.<p>Thank you.
======
twobyfour
Neither, put a real URL in there so people can a) use your site without JS and
b) open links in new tabs. Use e.preventDefault().

If you can't put a URL in it, use a different element. Why can't you use
<button>? It's definitely the correct element for something that takes an
action. In a pinch, fall back on <span>?

~~~
optikals
Thank you for your help, Sir.

------
savethefuture
There is no "correct" way to do anything in web, I would say its generally
best practice or personal preference. I tend to use # and target the tag with
a data attribute and use e.preventDefault(). #! from what I've seen is used
for handling client-side routing. Any tag can be used as a button in
javascript, as long as you target it correctly.

~~~
optikals
Thank you, Sir! And please, save the Future.

