简体   繁体   中英

How can I capture the internal LAN IP address using javascript

First post need some help. I'm building an internal network that has a series of iPads that are being used to control my house. I'm building a website that is using javascript to send out serial control commands to different devices connected to my server. I want to have 5 ipads in 5 different rooms. Each with a home page of different controls. So the controls for my bedroom are going to be different than the controls for my living room etc...However on each of my ipads I"m going to have a nav bar that will allow me to page jump to a different room for control. The thing i'm stuck on is I'd like to be able to click on a home button on any of the pages in any of the rooms and have that room's ipad be automatically redirected back to the home page that is specific to the ipad in that room. The only way I can see to do this is to set a static ip on each ipad and have the home button go back to index.html where the ip address of the requesting ipad is read and then redirected to teh correct home page for that ipad based on its static ip address. The kicker is I can't use php. I can use .asp but I don't know asp. If someone can help that would be fantastic. I grabbed a code snippet from a different post but the result keeps giving me my external IP which is worthless for what I'm trying to do. see below. The other thought I had wa s registering each ipad on first connect via a form with a specific name and saving that info as a cookie. Each time the ipad goes to index.html the cookie is read and then redirected accordingly.

<script type="text/javascript" language="javascript">
  function myIP() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
}
    else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

    xmlhttp.open("GET","http://jsonip.appspot.com/",false);
    xmlhttp.send();

    hostipInfo = xmlhttp.responseText;
    obj = JSON.parse(hostipInfo);
    document.getElementById("IP").value=obj.ip;
    document.getElementById("ADDRESS").value=obj.address;
}
</script>
</head>

<body onload="myIP()">
    IP :<input type="text" id="IP" name="IP" />
ADDRESS :<input type="text" id="ADDRESS" name="ADDRESS" />

Deciding which controls to display based on an external factor (the IP) is a bit inflexible and maybe to much trouble for little gain.

I more friendly way would be a little button/checkbox on your page to indicate which controls belong to the device. Effectively setting that page as the homepage.

Just like @Plato said in his comment you could use cookies, local- or session storage to persist the configuration state. The best bet is to use localStorage since it doesn't expire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM