首先声明,此篇文章纯属标题党。今日QQ群里探讨用JS获取客户端mac地址的方法,众说纷纭,有说行有说不行的,闲着没事儿研究了一下,js确实可以获取到客户端当前电脑的mac地址,代码如下,但这段代码放到服务器上面貌似就不行了,会提示 locator.ConnectServer(); 错误,不支持此属性或方法。具体原因估计跟js的权限有关。想通过js获取客户端mac地址的朋友们还是想其他办法吧!
<html> <head> <title>JS获取客户端MAC地址</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="generator" content="editplus" /> <script event="OnObjectReady(objObject,objAsyncContext)" for="foo"> if(objObject.IPEnabled!=null&&objObject.IPEnabled!="undefined"&&objObject.IPEnabled == true){ if(objObject.MACAddress!=null&&objObject.MACAddress!="undefined"&&objObject.DNSServerSearchOrder!=null) MACAddr=objObject.MACAddress; if(objObject.IPEnabled&&objObject.IPAddress(0)!=null&&objObject.IPAddress(0)!="undefined"&&objObject.DNSServerSearchOrder!=null) IPAddr=objObject.IPAddress(0); if(objObject.DNSHostName!=null&&objObject.DNSHostName!="undefined") sDNSName=objObject.DNSHostName; } </script> <script type="text/javascript"> var MACAddr; var IPAddr; var DomainAddr; var sDNSName; function init(){ var service=locator.ConnectServer(); service.Security_.ImpersonationLevel=3; service.InstancesOfAsync(foo,'Win32_NetworkAdapterConfiguration'); } function getMac(){ document.getElementById('txtMac').value=unescape(MACAddr); } </script> </head> <body onLoad="init()"> <object id="locator" classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" VIEWASTEXT></object> <object id="foo" classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223"></object> <input type="text" id="txtMac" /> <input type="button" id="btn" value="获取Mac地址" onClick="getMac()" /> </body> </html>