TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Href=“#” VS href=“JavaScript:;”

2 pointsby optikalsalmost 8 years ago
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.

2 comments

twobyfouralmost 8 years ago
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().<p>If you can&#x27;t put a URL in it, use a different element. Why can&#x27;t you use &lt;button&gt;? It&#x27;s definitely the correct element for something that takes an action. In a pinch, fall back on &lt;span&gt;?
评论 #14748108 未加载
savethefuturealmost 8 years ago
There is no &quot;correct&quot; 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&#x27;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.
评论 #14706376 未加载