How to Check for a Hash in a URL in JavaScript

Anika Tabassum Era Feb 15, 2024
  1. Check for a Hash in Any URL in JavaScript
  2. Check for a Hash in Current URL in JavaScript
How to Check for a Hash in a URL in JavaScript

In JavaScript, we have the most straightforward way to check if the URL we are working on has a hash (#), that is, by using window.location.hash, but this is for a dynamic purpose. It draws out the current websites’ information.

Also, we have other ways to check if any given URL consists of a hash; we take the URL as a string and check its indexes to find the hash. Again, we can use the split() method to check if our URL has a hash.

In the following examples, we will get a preview of these along with a jQuery solution. Let’s dig in!

Check for a Hash in Any URL in JavaScript

Here, we will see two instances that will cover how we can get the presence of a hash from any predefined URL. The following contains the indexOf() and split() methods for solving the case.

Use indexOf to Check for a Hash in Any URL in JavaScript

The URL we will consider has to be set as a string as the indexOf method plays in the case of strings. So, we will set a conditional statement here to give us our desired result, and we do get the proper fit.

Let’s check the code lines.

Code Snippet:

var url = 'http://www.google.sk/foo?boo=123baz';

if (url.indexOf('#') !== -1) {
  console.log('got hash')
} else {
  console.log('No hash');
}

Output:

Use indexOf to Get Trace of Hash

Use the split() Method to Check for a Hash in Any URL in JavaScript

In this case, we will again consider a URL as a string. Later, we will use the split() method to get the token if we have a hash in our case, and according to conditions, we do get a hash.

The code fence will demonstrate clearly.

Code Snippet:

var url = 'https://mail.google.com/mail/u/0/#inbox';
var parts = url.split('#');
if (parts.length > 1) {
  console.log('Found hash');
}

Output:

Use split() Method to Get Hash

Check for a Hash in Current URL in JavaScript

We will focus on the current website and its address. By fetching the URL, we will check the hash portion, and if we find it, this will result in an affirmation word; else, a negation.

Use window.location.hash to Check for a Hash in Current URL in JavaScript

The basic window.location.hash grabs the current URL and checks if there are any findings of hash. Then, we set up a condition to bring us an answer.

Code Snippet:

var hash = window.location.hash;
if (hash) {
  console.log('hash found');
} else {
  console.log('hash not found')
}

Output:

Use window.location.hash to Get Hash

Use the jQuery .prop() Method to Check for a Hash in Current URL in JavaScript

Similarly, here we used $(location) that by default fetches the current window we are working on. Then, the .prop() method adds up the condition to look for any available hash in the URL.

Code Snippet:

var hash = $(location).prop('hash');
if (hash) {
  console.log('hash found');
} else {
  console.log('no hash')
}

Output:

Use jQuery .prop() to Get Hash

Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook

Related Article - JavaScript Hash