Live data from Hacker News

Ask HN: the current state of the empty # in 'a href'

news.ycombinator.com

1–10 of 15 posts

Ask HN: the current state of the empty # in 'a href'

#1
What is your take on the a href='#' in web applications?

It used to be and may still be very well be today the 'good practice' to always have a # at least in the HREF. Empty or no HREF could cause issues with some browsers I was told.

Today it raises other problems. Those # litter web applications making link sharing duplicates in news site (ie. .com/# is different than .com/ but shouldn't), it also makes statistics skewed in some stats services.

I personally hate to see those empty domain.com/#

Note I have nothing against anchors actually being used as anchors, ie domain.com/#somesection. That's not what I'm referring to.

It also annoys me off to have those littering # in my browser history (ie. when you type in the url bar).

Is there an alternative? Using everywhere can also cause issues in some browsers I read, with some browser using the content of the button tag as labels while some others use the value attribute.

Is using A with no href still SIN today?

And don't tell me about HTML5. If it's not everywhere it's not on my cards.

Thanks.

Re: Ask HN: the current state of the empty # in 'a href'

#3
The standard is to use # in anchor tags that don't link anywhere that get hijacked with javascript and typically using jquery use e.preventDefault() which will stop the # from going into the address bar. You can also use return false. But e.preventDefault() is best practice. You need the # in the href because without it older browsers will not act like its a link which will prevent :hover from working. The # signs you are seeing all over the web are probably people who are not implementing preventDefault or return false. You could also be seeing them for sites that attempt to use something like jquery address for ajax navigation. If you are attempting to copy and share one of these links and it is not working then the developer didn't set up the ajax state management correctly for the particular app you are using. On a side note, you should probably leave these types of programming specific questions for stack overflow. I wouldn't consider this appropriate for hacker news in my opinion. Also you will probably get a larger response for this type of question on stack overflow. Heck, it has probably already been asked and answered multiple times on there.

Re: Ask HN: the current state of the empty # in 'a href'

#6
post #2

I use A without href only when I'm making an actual anchor A name. When I use href=#, I always have javascript onclick return false. This won't stop crawlers but it keeps the user's history and address bar clean.

Another approach is href="javascript:;". Not as nice in your status bar but slightly less code and you don't need a big JS lib to implement it.

(preventDefault is preferable though.)

Re: Ask HN: the current state of the empty # in 'a href'

#7
post #2

I use A without href only when I'm making an actual anchor A name. When I use href=#, I always have javascript onclick return false. This won't stop crawlers but it keeps the user's history and address bar clean.

what about href='' and in the jquery onclick function, return false at the end?

Re: Ask HN: the current state of the empty # in 'a href'

#8

The standard is to use # in anchor tags that don't link anywhere that get hijacked with javascript and typically using jquery use e.preventDefault() which will stop the # from going into the address bar. You can also use return false. But e.preventDefault() is best practice. You need the # in the href because without it older browsers will not act like its a link which will prevent :hover from working. The # signs yo…

All good points. Thanks.
Post reply on HN