How to check with javascript if connection is local host?

前端 未结 12 1849
孤独总比滥情好
孤独总比滥情好 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:35

    An easy way to do this would be to just check the hostname against localhost or check your custom domain name against a substring, in this case ".local" urls, such as http://testsite.local

    var myUrlPattern = '.local';
    if (window.location.hostname === "localhost" || location.hostname === "127.0.0.1" || window.location.hostname.indexOf(myUrlPattern) >= 0) {
        alert("It's a local server!");
    }
    

提交回复
热议问题