SOLVED! [HTML] Need a way to create a URL from an IP address.

MaxDepth

Diamond Member
Jun 12, 2001
8,757
43
91
</sigh>

So I have been searching for this but I wonder if I am searching incorrectly on the web, maybe I'm not using the right words.

It's pretty simple. I have this coding to query a Windows computer of their IP address:
Code:
<script>
           var objLocator = new ActiveXObject("WbemScripting.SWBemLocator");
           var objService = objLocator.ConnectServer(".","root\\CIMV2");
           instances = objService.ExecQuery(
           "SELECT Caption, IPAddress, MACAddress "
           + "FROM Win32_NetworkAdapterConfiguration");
           e = new Enumerator(instances);

           var Text = ""
           for(;!e.atEnd();e.moveNext())
           {
           nic = e.item();
           if(nic.IPAddress!=null)
           {
           var ipArray = VBArray(nic.IPAddress).toArray();
           // Text = Text + nic.Caption+"--"+nic.MACAddress+"--"+ipArray[0]+"<br>";
           Text = Text + ipArray[0]+"<br>";
           }
           }
           document.write(Text);
</script>

What I'd like to do is pull the third octet from Text and use it as a URL. Basically, I'd first (I wish I could just use grep...) get the third octet by:
Code:
Function CodeTML(Text As String) {
    Dim MyArray() As String
    MyArray = Split(Text, ".")
    CodeTML = MyArray(2)
    }

But I'm not getting that to work. But if I did, I'd like to then put the variable into an HTML page anchor tag, which I hope is right:
Code:
<a class="w3-bar-item w3-button w3-text-white" href="pages/"+'CodeTML'+.html" target="_blank">Support</a>

I am far off? Thank you for any help.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
The WMI activex objects are not safe for scripting in a browser, meaning your code will never run in a website. It will run in an HTA application though.

You cannot use a javascript variable in the href property of a link. Instead use javascript to set the href of the anchor or use document.write or document.append to add the anchor tag dynamically.
 

purbeast0

No Lifer
Sep 13, 2001
53,498
6,336
126
I'm not sure what language that is or how you have to do it, but that is very easily done in javascript. This only took a couple minutes. There's no error checking or anything though, always assumes IP address format of 4 parts.

EDIT:

This is redacted unreal, I can't even post a link to jsfiddle or tinyurl trying to make it work. Go to jsfiddle homepage and just add this ID to the end:

fwj7xr8y

You can't link to https://jsfiddle.net/fwj7xr8y/? I can. Swearing also removed. -- Programming Moderator Ken g6
 
Last edited by a moderator:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,589
4,496
75
Futher JSFiddle issues moved to this thread in Technical Forum Issues -- Programming Moderator Ken g6
 

MaxDepth

Diamond Member
Jun 12, 2001
8,757
43
91
Thanks, guys. I got it to work finally. I know ActiveX is the devil's chew toy but it is the only way to script the discovery of the client IP address using Windows 7 (or Win10) and IE 11. Unfortunately I don't get a choice or say in those matters. But they would allow this one ActiveX because the function is so limited.

So here is my IP detection script, it goes in before the HTML <body> tag:
Code:
<script>
           var objLocator = new ActiveXObject("WbemScripting.SWBemLocator");
           var objService = objLocator.ConnectServer(".","root\\CIMV2");
           instances = objService.ExecQuery(
           "SELECT Caption, IPAddress, MACAddress "
           + "FROM Win32_NetworkAdapterConfiguration");
           e = new Enumerator(instances);

           var IPAddy = ""
           for(;!e.atEnd();e.moveNext())
           {
           nic = e.item();
           if(nic.IPAddress!=null)
           {
           var ipArray = VBArray(nic.IPAddress).toArray();
           IPAddy = IPAddy + ipArray[0]+"<br>";
           }
           }
</script>

Here is a script that finds the third octet and creates the HTML link from that number.
Code:
<script>
  var input = IPAddy;
  var split = input.split('.');
  var TPage = split[2];
  var link = document.getElementById('link');
  link.href = "pages/" + TPage + ".html";
  console.log(link);
</script>

However, I cannot just place that last script at the top with the other scripts for it to work. The one thing I was butting my head against was the fact that I need to place the script AFTER the HTML link, not before. So here is that third octet script in the body of the <div> tag, after the link.

Code:
<div>
  <a class="w3-bar-item w3-button w3-text-white" id="link" target="bigWindow">Your Computer Information</a>
  <script>
     var input = IPAddy;
     var split = input.split('.');
     var TPage = split[2];
     var link = document.getElementById('link');
     link.href = "pages/" + TPage + ".html";
     console.log(link);
  </script>
</div>

Thank you both for giving your input.