What are fragment URLs and why to use them?

Nilesh picture Nilesh · Jun 23, 2015 · Viewed 29.4k times · Source

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?

Answer

PockeTiger picture PockeTiger · Jun 23, 2015

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.