Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *