A Fresh Look at What href Can Do

Some HTML behaviors are easy to overlook simply because we learned them early, before we knew what questions to ask. Jim Nielsen prompted a revisit of URL patterns in the href attribute, and the article is a reminder that these capabilities are worth more than a passing glance — even for developers who use them regularly.

<a href="#"> <!-- Scrolls to the top of a document -->
<a href=""> <!-- Reloads the current page, preserving the search string but removing the hash string (if present). -->
<a href="."> <!-- Reloads the current page, removing both the search and hash strings -->
<a href="?"> <!-- Reloads the current page, removing both the search and hash strings (keeps `?`) -->
<a href="data:"> <!-- Link to data URLs, like text fragments -->
<a href="video.mp4#t=10,20">  <!-- Links to specific parts of a media file -->

The Detail Worth Catching

The post digs deeper than a casual overview, working against the specification and stress-testing various configurations. One nugget in particular stood out:

But I’m writing because #top will also scroll to the top if there isn’t another element with id="top" in the document. I didn’t know that.

Neither did the author of the original note, who recommends reading Nielsen’s full write-up for the complete picture.

Direct Link →