How to check with javascript if connection is local host?

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

    Shortest form using same mechanic as other scripts:

    if ( ["localhost", "127.0.0.1", ""].includes(window.location.hostname) ) {
         console.log("It's local host !");
    }
    
    0 讨论(0)
  • 2020-12-23 09:32
    const LOCAL_DOMAINS = [ "localhost", "127.0.0.1" ];
    
    /* offline || development */
    if ( LOCAL_DOMAINS.includes(location.hostname) )
    {
        BASE_URL_PUBLIC = location.hostname + "/folder/website/"; // your project folder
    }
    
    /* online || production */
    else
    {
        BASE_URL_PUBLIC = location.hostname;
    }
    
    0 讨论(0)
  • 2020-12-23 09:33

    The above answers mostly solve the problem but...

    • What if localhost isn't necessarily 'localhost/'?
    • What if you want to do FE validation during development?
    • What if you want different behaviors during dev
      (fe validation, be validation, no validation)

    One solution is to set the location hash and check it.

    http://myname.foo.com/form.html#devValidation

    You could add unlimited options with a switch

    switch(location.hash) {}
        case '#devValidation':
            // log the results and post the form
            break;
        case '#beValidation':
            // skip front end validation entirely
            break;
        case '#noValidation':
            // skip all validation $('[name=validationType']).val('novalidation');
            break;
        case '#feValidation':
        default:
            // do fe validation
            break;
    }
    
    0 讨论(0)
  • 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!");
    }
    
    0 讨论(0)
  • 2020-12-23 09:35

    Based on the above comments th following regular expression has helped me to verify if the url is 'localhost', any IP adress IPv4 or IPv6.

    window.location.hostname.match(/localhost|[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}|::1|\.local|^$/gi)
    
    0 讨论(0)
  • 2020-12-23 09:38

    That's how it get checked in React, register service worker, good way to check if you are on localhost by checking hostname, including localhost and IPv6, and matching start with 127:

    const isLocalhost = Boolean(
        window.location.hostname === 'localhost' ||
        // [::1] is the IPv6 localhost address.
        window.location.hostname === '[::1]' ||
        // 127.0.0.1/8 is considered localhost for IPv4.
        window.location.hostname.match(
            /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
        )
    );
    
    0 讨论(0)
提交回复
热议问题