== .fragment ==
====== $p.url.fragment ====== This module encapsulates access to the page URL’s fragment part, i.e. the part after a hash (''#'') symbol.
== .fragment ==
===== 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 ''[[mdn>Web/API/Event/preventDefault|.preventDefault()]]'' method if you want to avoid the URL change to trigger default browser functionalities.
===== See also ===== * [[framework:page:p-core:index|$p]] (page core) * [[framework:page:p-url:index|$p.url]] (URL core) ===== More information ===== * [[mdn>Web/API/URL/hash|URL: hash property]] on MDN.