Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are fragment URLs and why to use them?

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?

like image 608
Nilesh Avatar asked Jun 23 '15 08:06

Nilesh


People also ask

What are URL fragments used for?

A hash sign (#) in a URL is referred to as a fragment. Historically, URL fragments have been used to automatically set the browser's scroll position to a predefined location in the web page. In that sense, if a URL refers to a document, then the fragment refers to a specific subsection of that document.

What is URL hash fragment?

The primary resource is identified by a Uniform Resource Identifier (URI), and the fragment identifier points to the subordinate resource. The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.

Are URL fragments sent to server?

Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.

Can a URL have multiple fragments?

URL parameters are used to pass information to the page. URL fragments are used from pointing and scrolling to elements on the HTML document. A URL can have multiple parameters. A URL cannot have more than one fragment.


2 Answers

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.

like image 200
PockeTiger Avatar answered Oct 20 '22 04:10

PockeTiger


It's unable to read it by php. It uses by client side (browser) for hash navigation, but you can write JS code to handle hash change and send async request to your server side (php) and display result on your page.

like image 33
Sergey B. Avatar answered Oct 20 '22 04:10

Sergey B.