What are fragment URLs and why to use them?

37,844

A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page.

In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.

There are a few things about the fragments, the most important may be that they aren't sent in HTTP request messages but you can find some more info about them on this page.

Javascript can manipulate fragments on the current page which can be used to to add history entries for a page without forcing a complete reload.

Share:
37,844
Nilesh
Author by

Nilesh

Updated on September 26, 2020

Comments

  • Nilesh
    Nilesh almost 4 years

    I am new to PHP Development.

    Today I came across the interesting topic of URL fragments, specifically the # part of URLs.

    I searched and found that it's like

    www.example.com/foo.html#bar
    

    But I don't understand why this #bar is needed. Or how to read it in PHP?

  • David Spector
    David Spector over 4 years
    While parse_url works after you have a complete URL, PHP cannot obtain a complete URL, including the anchor name. Since anything JavaScript and the DOM can manipulate might need to be processed by PHP, it doesn't make sense that PHP cannot obtain the entire URL with which it is called. Seems to be a very big but in PHP, unless I'm missing something.
  • Chris
    Chris over 3 years
    @DavidSpector I am not sure how this is a bug in PHP. Per definition, a fragment is not sent to the server. As PHP runs on the server, there is no way PHP could ever access the fragment of any URL from which it is called. Just the same as any other server-based website framework like .NET, Ruby, node, etc.