/

Accessing the Fragment Part of a URL

Accessing the Fragment Part of a URL

Learn how to retrieve the value of the fragment portion in a URL for programming purposes.

Oftentimes, there is a requirement to access the fragment part of a URL programmatically. The fragment is the portion of the URL that follows the hash symbol (#).

For instance, given the URL index.html#second, the desired outcome is to extract the fragment value “second”.

Here is a simple approach to accomplish this:

1
const fragment = window.location.hash;

This code snippet assigns the value of the fragment to the variable fragment using the window.location.hash property.

By using this method, you can easily obtain the fragment value from a URL in your JavaScript code.

Tags: programming, URL fragment, JavaScript