Check If A URL Is Localhost In JavaScript

Check if a URL is Localhost in JavaScript

Sometimes during web application development, you will encounter a request to check if a URL is localhost. This article will present two solutions using the includes() and indexOf() methods to check if a URL is localhost in JavaScript. Let’s go.

How to check if a URL is localhost in JavaScript

The local network usually contains the ‘localhost’ string or the ‘127.0.0.1’ IP address. We will rely on these characteristics to perform a check to see if a URL is localhost in JavaScript.

We will describe two solutions, respectively, using includes() and indexOf() methods to check if a URL is localhost.

Use the includes() method

The includes() method returns a boolean value (true if the string contains the value, otherwise false). It is used to check if a string contains something else. The includes() method is case-sensitive.

Syntax:

includes(searchvalue, start)

Parameters:

searchvalue: String used for search. 

start: Position to start the search.

Note: If not set, the ‘start’ parameter will default to 0.

Example:

// Check If A URL Is Localhost
function checkLocalhost(urlString) {
	return urlString.includes("localhost") || urlString.includes("127.0.0.1");
}

let url = "http://localhost:8000";

if (checkLocalhost(url)) {
	console.log("The '" + url + "' url is localhost");
} else {
	console.log("The '" + url + "' url is not localhost");
}

Output:

The 'http://localhost:8000' url is localhost

We have built the checkLocalhost() function, which uses the includes() method and will return true if the input URL string contains any of the two substrings ‘localhost’ or ‘127.0.0.1’.

Use the indexOf() method

The indexOf() method returns the index of the first occurrence of a substring in a string. Otherwise, the indexOf() method will return -1. The indexOf() method is case-sensitive.

Syntax:

indexOf(searchvalue, start)

Parameters:

searchvalue: the value used for search. 

start: a position to start the search. The default is 0.

We will do the same as the previous example to check if a URL is localhost using the indexOf() method. However, the checkLocalhost() function will return the index of the occurrence first of any of the two substrings ‘localhost’ or ‘127.0.0.1’.

Example:

// Check If A URL Is Localhost
function checkLocalhost(urlString) {
	return urlString.indexOf("localhost") || urlString.indexOf("127.0.0.1");
}

let url = "http://localhost:8000";

if (checkLocalhost(url) >= 0) {
	console.log("The '" + url + "' url is localhost");
} else {
	console.log("The '" + url + "' url is not localhost");
}

Output:

The 'http://localhost:8000' url is localhost

Summary

Use the best solution to check if a URL is localhost in JavaScript. Note that both solutions we give in this article perform checking if a URL is localhost without helping you to check if the connection is localhost. Thanks for reading.

Maybe you are interested:

Leave a Reply

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