Johan Johan - 4 months ago 9
HTML Question

Using # at the beginning of an href hyperlink

I know that

#
character within links of hyperlink tag
href
is used to scroll the pointed page to the specified element-id once opened:

href="//site.com/#some-id"


when used solely, means to just scroll up to the top of the current page:

href="#"


but what about when it is attached to the beginning of a link?

href="#/phones/phoneid


will it scroll the pointed page to its top once opened?

Answer

The # in a URI is used to indicate an in-page anchor on a given page.

I know that # character within links of hyperlink tag href is used to scroll the pointed page to the specified element-id once opened:

href="//site.com/#some-id"

It's not so much that it "opens" the page and then "scrolls" to the anchor. It opens the page at the anchor - the anchor is part of the address.

when used solely, means to just scroll up to the top of the current page:

href="#"

Yes. Kind of. But that's because you haven't stated the name of the anchor and the browser can't find it - so the browser doesn't take you to anywhere below the top of the page.

but what about when it is attached to the beginning of a link?

href="#/phones/phoneid/"

will it scroll the pointed page to its top once opened?

Well, there's no need, is there?

href="/phones/phoneid/"

will already take you to the top of the /phones/phoneid/ page.

In short, a hash-fragment is part of a web address.

The hash-fragment consists of two parts:

  1. The hash (#)
  2. The name of the anchor

Clicking on a URI containing a hash-fragment opens the indicated page at the indicated anchor.

Clicking on a URI which does not contain a hash-fragment opens the indicated page at the top.

If a URI contains a hash-fragment which has an invalid name (or which isn't named at all), the browser won't be able to find the anchor and will default to opening the indicated page at the top.