Sometimes, we want to check with JavaScript if connection is local host.
In this article, we’ll look at how to check with JavaScript if connection is local host.
How to check with JavaScript if connection is local host?
To check with JavaScript if connection is local host, we can check the value of location.hostname
.
For instance, we write
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
alert("It's a local server!");
}
to check if location.hostname
is 'localhost'
or '127.0.0.1'
.
If either of these are true
, then we’re running our app on localhost.
Conclusion
To check with JavaScript if connection is local host, we can check the value of location.hostname
.