Namespace: Urls:urlAnchor

Urls:urlAnchor

Source:

Methods

urlAnchor(urlopt, withCaretopt, nullable) → {String|null}

Returns the currently set URL-Anchor on given URL.

Theoretically, this function also works with any other string containing a hash (as long as there is "#" included), since this implementation does not lean on "new URL()", but is a simple string operation.

In comparison to "location.hash", this function actually decodes the hash automatically.

Parameters:
Name Type Attributes Default Description
url String | URL <optional>
null

the url, in which to search for a hash, uses current url if nullish

withCaret Boolean <optional>
<nullable>
false

defines if the returned anchor value should contain leading "#"

Source:
Throws:

error if given url is not usable

Returns:
current anchor value or null if no anchor was found
Type
String | null
Example
const anchorWithoutCaret = urlAnchor('https://foobar.com#test');
=> 'test'
const hrefAnchorWithCaret = urlAnchor(linkElement.getAttribute('href'), true);
=> '#test'
const decodedAnchorFromLocation = urlAnchor(window.location.hash);