The fragment identifier in a URL provides further refinement of the specification of the resource, including additional identifying information. It provides directions to a secondary resource related to such main resource, such as the section in an article or a time-point in a video.
As usual, its semantics vary depending on the scheme, authority, path, and media type of the resource. In HTML, fragments are used as internal page references. This usage is called a "named anchor," referring to a section within a web page.
The URL below contains the fragment "section2":
https://www.example.com/index.html#section2 \____/\_______________/\_________/\_______/ scheme authority path fragment
A fragment appearing in a URL is always preceded by the number sign ('#'). A This makes a URL with a fragment of zero length distinguishable from a URL with no fragment.
The fragment grammar is defined as follows:
Table 1.25. Fragment BNF
fragment = *( pchar / "/" / "?" ) relative-ref = relative-part [ "?" query ] [ "#" fragment ] URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] URI-reference = URI / relative-ref |
The functions for inspecting the fragment in a url_view
are as follows:
Table 1.26. Fragment Observers
Function |
Description |
---|---|
Return true if a fragment is present |
|
Return the fragment as a percent-encoded string. |
|
Return the fragment as a string with percent-decoding applied. |
Analogous to other components, the functions encoded_fragment
and fragment
can be used to obtain the fragment from a url_view
:
Code |
Output |
---|---|
urls::string_view s = "https://www.example.com/index.html#section%202"; urls::url_view u = urls::parse_uri(s).value(); std::cout << u << "\n" "has fragment: " << u.has_fragment() << "\n" "encoded fragment: " << u.encoded_fragment() << "\n" "fragment: " << u.fragment() << "\n"; |
https://www.example.com/index.html#section%202 has fragment: 1 encoded fragment: section%202 fragment: section 2 |
These functions do not throw. If the URL has no fragment, encoded_fragment
returns an empty string. The function has_fragment
can be used to determine whether this empty string means there is no fragment
or an empty fragment string in the URL.
Code |
Output |
---|---|
urls::string_view s = "https://www.example.com/index.html#"; urls::url_view u = urls::parse_uri(s).value(); std::cout << u << "\n" "has fragment: " << u.has_fragment() << "\n" "encoded fragment: " << u.encoded_fragment() << "\n" "fragment: " << u.fragment() << "\n"; |
https://www.example.com/index.html# has fragment: 1 encoded fragment: fragment: |
urls::string_view s = "https://www.example.com/index.html"; urls::url_view u = urls::parse_uri(s).value(); std::cout << u << "\n" "has fragment: " << u.has_fragment() << "\n" "encoded fragment: " << u.encoded_fragment() << "\n" "fragment: " << u.fragment() << "\n"; |
https://www.example.com/index.html has fragment: 0 encoded fragment: fragment: |
URL fragments are usually interpreted as a single string.
Component |
Value |
---|---|
URL |
|
Has Fragment |
Yes |
Encoded Fragment |
|
Fragment |
|
The URL fragment might also be empty or absent.
Component |
Value |
---|---|
URL |
|
Has Fragment |
Yes |
Encoded Fragment |
(empty) |
Fragment |
(empty) |
Component |
Value |
---|---|
URL |
|
Has Fragment |
No |
Encoded Fragment |
(No fragment) |
Fragment |
(No fragment) |
The URL reserved characters :
, @
, ?
,
and /
may appear unencoded with URL fragments, as they are not
ambiguous with other URL components.
Component |
Value |
---|---|
URL |
|
Has Fragment |
Yes |
Encoded Fragment |
|
Fragment |
|