no pain no gain

hard working

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  如果获取不到则需要对IE浏览器进行设置。具体设置如下:

请设置IE浏览器-工具-安全-自定义级别-ActiveX第三项‘对没有标记为安全的ActiveX控件进行初始化和脚本运行’为启用,点击确定后刷新重新登录!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript获取客户端IP</title>
<script type="text/javascript" language="javascript">
<!--
function GetLocalIPAddress()
{
    var obj = null;
    var rslt = "";
    try
    {
        obj = new ActiveXObject("rcbdyctl.Setting");
        rslt = obj.GetIPAddress;
        obj = null;
    }
    catch(e)
    {
        //异常发生
    }
    return rslt;
}

function getMac(){
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled =True");
      var e = new Enumerator (properties);
      {
            var p = e.item();
            var mac = p.MACAddress;
            return mac
      }
}
//-->

function init () {
    var ip = GetLocalIPAddress();
    var mac  = getMac();
    fm.clientIP.value  = ip;
    fm.clientMAC.value  = mac;
} 

</script>
</head>
<body onload="init();">
<form name = 'fm'>
        通过JavaScript获取的IP信息:<input type ='text' name = 'clientIP'><br>
        通过JavaScript获取的MAC信息:<input type='text'name  = 'clientMAC'> 
        通过JSP获取的IP信息:<%= request.getRemoteAddr() %>
</form>
</body>
</html>

 

posted on 2013-11-25 09:10  龚明秋  阅读(9840)  评论(4编辑  收藏  举报