How to check if a URL is localhost in JavaScript

1 Answer

0 votes
function isLocalhost(url) {
  	return url.includes('localhost') || url.includes('127.0.0.1');
}

console.log(isLocalhost('http://localhost:8000/'));

console.log(isLocalhost('http://127.0.0.1:1234/test/'));

console.log(isLocalhost('http://seek4info.com'));




  
/*
run:
  
true
true
false
  
*/

 



answered Apr 28, 2022 by avibootz

Related questions

1 answer 165 views
1 answer 142 views
1 answer 134 views
1 answer 130 views
1 answer 156 views
1 answer 138 views
...