How to check with javascript if connection is local host?

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

    The location.hostname variable gives you the current host. That should be enough for you to determine which environment you are in.

    if (location.hostname === "localhost" || location.hostname === "127.0.0.1")
        alert("It's a local server!");
    

提交回复
热议问题