Add and Change URL Hash (#) in JavaScript Without Reloading

Robin
Updated on October 6, 2022

You can add hash value to a URL using JavaScript to navigate a user from one section to another without reloading the page. You can also change the existing URL hash.

JavaScript has the location.hash property that can add or change the hash value in the URL. The history API can set a hash to the current URL using the pushState() method in JavaScript. If the URL is a string, the URL() constructor can attach the value to the string.

You use this technique to move one section to another on a web page. When we create a table of contents, we set the id of different sections as the hash value.

In this article, I will show 3 methods to add or change the hash value of a URL in JavaScript without reloading the page.

Add JavaScript URL Hash Using location.hash Property

The hash property from the location object can return the existing hash value from the current URL in a browser. You can also check for the hash (#) value in a URL using JavaScript and set a new value to change the hash.

The location object contains information about the current URL. That's why we can modify the current URL using the properties and methods of this object.

          location.hash = 'reviews';
// https://example.com#reviews
        

I am adding "reviews" to the URL as a hash value by setting it to the location.hash property. It is a very easy process.

If your current URL is "https://example.com" in the browser, this line of code will attach "#reviews" at the end of that URL. Like, https://example.com/#reviews

This property not only adds value but also can get the existing hash value from a URL in JavaScript. We can extract it from the current URL using location.hash property.

Add and Change URL Hash (#) in JavaScript Without Reloading

Change URL Hash With History API in JavaScript

JavaScript history API has several methods to add hash value to a URL. They also add value to the current URL in a web browser.

Methods like pushState() and replaceState() can change the URL without reloading the page in JavaScript. When we change the URL, we will attach the hash value to it.

          history.pushState('', '', '#reviews');
        

This line of code will add the "#reviews" to the current URL. I am using pushState() method from the history object. You can also use the replaceState() method.

But in that case, you can't go back to the previous page by pressing the back button in your browser. Both of them will set the hash value.

Update Hash Value Using URL() Constructor

Sometimes we might have a URL string that contains a hash value. We want to add the hash value to that string. It is very easy to add something to a string.

You can use the string concatenation technique to join a piece of string to another string.

          const url = 'https://example.com';

const newURL = `${url}#reviews`;
// https://example.com/#reviews
        

I am adding "#reviews" to the url string to get a new URL with the hash value. I am using the template literal syntax in JavaScript.

You can also do it using the URL() constructor function in JavaScript. It accepts an URL string and returns an object. This object will contain information about that string.

          const url = 'https://example.com';

const urlObj = new URL(url);

urlObj.hash = '#reviews';

console.log(urlObj.href);
// https://example.com/#reviews
        

I am passing the url string to the constructor and it returns the object. This object has a hash property that can add a hash value to the URL.

Now, you can access the updated URL from the urlObj.href property. You can redirect to the new URL in JavaScript to get the content.

It might look like more work compared to the previous technique. But it is more helpful than the string concatenation when we try to change the existing hash value.

We can easily add something at the end of a string. But to change something, at first we have to remove that part. Then we can attach a new string.

          const url = 'https://example.com/#reviews';

const urlObj = new URL(url);

urlObj.hash = '#comments';

console.log(urlObj.href);
// https://example.com/#comments
        

As you can see, you have to follow the same process to change the hash value using URL() constructor. It is a very easy and simple way to modify the URL hash value.

Conclusion

There are many use cases of the hash value in a URL. We can take our users from one section to another without reloading the page. In this way, a user doesn't have to load the page again and again.

That's why it is important to know how to work with URL hash in JavaScript. You can also remove a hash from the URL using JavaScript.

You have learned 3 methods in this article. Now, you can use any one of them to add and change the hash value from a URL using JavaScript.

Related Posts