JS get the client IP address, Mac and host name 7 methods summary _javascript Tips

Source: Internet
Author: User
Tags get ip visibility

Today in the JS (JavaScript) to obtain the client IP applet, the Internet search, many in the current system and browsers are ineffective, very helpless, in Chrome, Firefox rarely get the direct use of ActiveX access to IP and other JS script. The following code is what I tested on all windowsNT5.0 and above systems, giving the code:

One, use JS to obtain the client IP several methods

Method one (for IE only and the client's IE allows Acitivex to run, via platform: xp,server03,2000).
Get client IP code:

Copy Code code 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 Ac Tivexobject ("Rcbdyctl.") Setting "); ip = osetting.getipaddress; if (Ip.length = = 0) {return "is not connected to the Internet";} osetting = null; }catch (e) {return IP;} return IP; } document.write (Getlocalipaddr () + "<br/>") </script>
</BODY>
</HTML>

Method Two (all platforms and browsers):
get the IP of the client in the network, provided the client is networked. The Sina interface is used.

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>javascript get client ip[use Sina interface]</title>
<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/>"); IP address in 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 provinces and cities of the IP address in the output interface data
Document.writeln ("Address type:" +ildata[3]+ "<br/>"); Of the IP address in the output interface data.
Document.writeln ("Address type:" +ildata[4]+ "<br/>"); Operator of IP address in output interface data
</script>
</body>

Method Three (all platforms and browsers):
use of the Sohu interface

Copy Code code 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 four: Pacific Internet IP Query interface:

http://whois.pconline.com.cn/?ip=0.0.0.0

Change the 0.0.0.0 to IP address, there are other irrelevant content on the page, which is to tell us which interfaces can be invoked, interface call parameters and usage methods, etc.

The above JS interface can be used to determine which city to belong to, direct display of the city's relevant information, for the need for city switching sites, the first time to judge the source of users is very helpful.

Second, use JS to obtain computer name, MAC address, LAN IP

Method one (for IE only and the client's IE allows Acitivex to run):
call the VBS script to get the computer name (some people do not 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 user name of the login.

Copy Code code 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 Two (ie only and the client's IE allows Acitivex to run):
get the name of the computer, the username and domain name of the login (if you joined the domain, show which domain your machine is in).

Copy Code code as follows:

<HTML>
<HEAD>
<TITLE>WMI Scripting Html</title>
</HEAD>
<BODY>
<script language=javascript>
var wshnetwork = new ActiveXObject ("Wscript.Network");
document.write ("Domain name =" + wshnetwork.userdomain+ "<br/>");
document.write ("Computer name =" + wshnetwork.computername+ "<br/>");
document.write ("Login username =" + wshnetwork.username+ "<br/>");
</script>
</BODY>
</HTML>

Method Three (ie only and the client's IE allows Acitivex to run):
be able to obtain LAN IP address, local Mac, and machine name (code source network).

Copy Code code as follows:

<title></title>
<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>
<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.