Today, I am working on JS (javascript) to obtain client IP. I searched the Internet and found that many of them are invalid in the current system and browser. I was helpless. In Chrome and FireFox, I rarely use ActiveX to obtain IP and other JS scripts. The following code is what I tested and passed on all Windows NT5.0 and above systems, and gave the code:
1. Several ways to obtain client IP using JS
Method 1 (only for IE and client IE allows ActiveX to run, through platform: XP, SERVER03, 2000).
Get the client IP code:
The code copy is as follows:
<HTML>
<HEAD>
<TITLE>GetLocalIP</TITLE>
</HEAD>
<BODY>
Get IP:
<script language="JavaScript"> function GetLocalIPAddr(){ var oSetting = null; var ip = null; try{ oSetting = new ActiveXObject("rcbdyctl.Setting"); ip = oSetting.GetIPAddress; if (ip.length == 0){ return "Not connected to the Internet"; } oSetting = null; }catch(e){ return ip; } return ip; } document.write(GetLocalIPAddr()+"<br/>") </script>
</BODY>
</HTML>
Method 2 (all platforms and browsers):
Obtaining the IP of the client on the network is the premise that the client must be connected to the Internet. The Sina interface is used.
The code copy is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript gets client IP [Use Sina Interface]</title>
</head>
<body>
<script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script> <!--Get interface data, pay attention to charset -->
<script type="text/javascript">
document.writeln("IP address: "+ILData[0]+"<br />"); //The IP address in the output interface data
document.writeln("Address type: "+ILData[1]+"<br />"); //The type of IP address in the output interface data
document.writeln("Address type: "+ILData[2]+"<br />"); //The province and city of outputting the IP address in the interface data
document.writeln("Address type: "+ILData[3]+"<br />"); //Output the IP address in the interface data
document.writeln("Address type: "+ILData[4]+"<br />"); //The operator of the IP address in the output interface data
</script>
</body>
</html>
Method 3 (all platforms and browsers):
Sohu interface used
The code copy is as follows:
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
document.write(returnCitySN["cip"]+','+returnCitySN["cname"])
</script>
Method 4: Pacific Computer Network IP query interface:
http://whois.pconline.com.cn/?ip=0.0.0.0
Replace 0.0.0.0 with IP address, and there are other irrelevant content on the page. These contents tell us which interfaces can be called, interface calls parameters and usage methods, etc.
Through the above js interface call, you can determine which city you belong to and directly display the relevant information of the city. For websites that require city switching, it is very helpful to determine the source of the user for the first time.
2. Use JS to obtain computer name, MAC address, and LAN IP
Method 1 (only for IE and client IE allows ActiveX to run):
Call the VBS script to get the computer name (some people don't know what the computer name is, the simple explanation is that it is the physical name of the machine rather than the user name you are using) and the login user name.
The code copy is as follows:
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var WshShell =new ActiveXObject("WScript.Shell");
document.write("Computer name = "+ WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")+"<br/>");
document.write("Login username= "+ WshShell.ExpandEnvironmentStrings("%USERNAME%")+"<br/>");
</script>
</BODY>
</HTML>
Method 2 (only for IE and client IE allows ActiveX to run):
Get the computer name, logged in username, and domain name (if you join the domain, it shows which domain your machine is in).
The code copy is as follows:
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var wshNetwork = new ActiveXObject("WScript.Network");
document.write("Domain= "+ wshNetwork.UserDomain+"<br/>");
document.write("Computer name = "+ wshNetwork.ComputerName+"<br/>");
document.write("Login username= "+ wshNetwork.UserName+"<br/>");
</script>
</BODY>
</HTML>
Method 3 (only for IE and client IE allows ActiveX to run):
You can obtain the LAN IP address, the MAC of the machine, and the machine name (code source from the network).
The code copy is as follows:
<html>
<head>
<title></title>
</head>
<body>
<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object>
<form name="myForm">
<br/>MAC address: <input type="text" name="macAddress">
<br/>IP address: <input type="text" name="ipAddress">
<br/>Host name: <input type="text" name="hostName">
</form>
</body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName="";
var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</script>
<script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</script>
<script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript">
myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;
</script>