例子,
<html>
<head>
<title>js获取客户端网卡IP地址与mac地址---www.jb200.com</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地址:<input type="text" name="macaddress">
<br/>ip地址:<input type="text" name="ipaddress">
<br/>主机名:<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" && objobject.dnsserversearchorder!=null)
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>
代码说明:
如果笔记本有多张网卡,会出现获取未联网或禁用网卡的信息。
解决办法:加限制条件:
objobject.dnsserversearchorder!=null
有了此限制条件后,就能取到联网客户端的网卡ip地址了。
注意,只能限制ie浏览器,而且activex必须要全部启用。