How to detect if we’re in the home page with JavaScript?

Sometimes, we want to detect if we’re in the home page with JavaScript.

In this article, we’ll look at how to detect if we’re in the home page with JavaScript.

How to detect if we’re in the home page with JavaScript?

To detect if we’re in the home page with JavaScript, we can check if window.location.origin is the same as window.location.href.

For instance, we write:

console.log(window.location.origin === window.location.href.slice(0, -1))

to check if window.location.origin is the same as window.location.href without the trailing slash.

Conclusion

To detect if we’re in the home page with JavaScript, we can check if window.location.origin is the same as window.location.href.