Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Fragment

The fragment provides a refinement of the resource specification usually interpreted as a single string. 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 points to the anchor "section2":

url_view u("https://www.example.com/index.html#section2");
assert(u.fragment() == "section2");

These functions do not throw. The URL fragment might also be empty or absent. If the URL has no fragment, these functions return 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.

assert(url_view("https://www.example.com/index.html#").has_fragment());
assert(!url_view("https://www.example.com/index.html").has_fragment());

The URL reserved characters :, @, ?, and / may appear unencoded with URL fragments, as they are not ambiguous with other URL components.

url_view u("https://www.example.com/index.html#code%20:a@b?c/d");
assert(u.fragment() == "code :a@b?c/d");

PrevUpHomeNext