Languages
[Edit]
EN

JavaScript - check if script is run on localhost in web browser

4 points
Created by:
Root-ssh
179400

In this article, we would like to show you how to check if the script is run on localhost in a web browser using JavaScript.

Quick solution:

if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
    console.log('This is a localhost.');
}

 

Practical example

In this section, we present a practical example of the above solution executed under localhost.

JavaScript - check if localhost
JavaScript - check if localhost

References

  1. location.hostname - Web APIs | MDN

Alternative titles

  1. JavaScript - check if localhost
  2. JavaScript - is localhost checking
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join