Skip to content

How to Check for a Hash Value in a URL Using JavaScript

The web page elements are modified depending on the hash (#) value of the current URL. You can get the hash value from the URL quickly by just running JavaScript code.

You can use the window.location.hash property to get the hash:

JavaScript: Get hash using window.location.hash

Output appears here after Run.

The window.location.hash property contains the fragment identifier, including the hash ‘#’ sign for the current page. For example:

http://w3docs.com/#foo

Here, the property would return the fragment identifier of #foo.

The window.location Property

The location interface represents the URL of the object to which it belongs. The changes that are done on it are reflected in the object to which it relates. The window.location is a reference to a location object representing the current URL of the document being displayed in that window.

Dual-run preview — compare with live Symfony routes.