How to check with javascript if connection is local host?

前端 未结 12 1850
孤独总比滥情好
孤独总比滥情好 2020-12-23 08:45

I want to have a check in my javascript if the page loading up is on my local machine.

The reason why I want to do that is that when I developing I like to make sure

12条回答
  •  温柔的废话
    2020-12-23 09:17

    This one also covers some common cases where local network IPs start with 10.0. or 192.168. or Bonjour like domain ending on .local:

    export function isLocalNetwork(hostname = window.location.hostname) {
      return (
        (['localhost', '127.0.0.1', '', '::1'].includes(hostname))
        || (hostname.startsWith('192.168.'))
        || (hostname.startsWith('10.0.'))
        || (hostname.endsWith('.local'))
      )
    }
    

提交回复
热议问题