Table of Contents

$p.url.fragment

This module encapsulates access to the page URL’s fragment part, i.e. the part after a hash (#) symbol.

Methods

The following methods are provided by this module:

.get()

$p.url.fragment.get()

Returns the current URL fragment as String, or an empty string if none exists.

.set()

$p.url.fragment.set(name)

Sets a new fragment to be shown in the address bar of the browser. This will also trigger any callbacks that are attached, e.g. via the .onChange() method.

.onChange()

$p.url.fragment.onChange(callback)

This method allows to attach a callback function that will be called when the URL fragment of the page changes.

Example:

$p.url.fragment.onChange( (e) => {
  console.log($p.url.fragment.get());
  e.preventDefault();
});
Notes:
  • Don’t forget to check for any URL fragment that is in place when the page is initially loaded, as this will not trigger the callback function.
  • Use the .preventDefault() method if you want to avoid the URL change to trigger default browser functionalities.

See also

More information