js获取客户端外网ip实现代码

发布时间:2019-08-14编辑:脚本学堂
本文介绍了js获取客户端ip地址的二个例子,学习下js取客户端ip的方法,有需要的朋友参考下。

例1,js获取客户端的IP地址
 

复制代码 代码示例:
var wwip="";
    $(function(){
        $(document).ready( function() {
            $.getjson( "http://smart-ip.net/geoip-json?callback=?",
                function(data){
                    alert( data.host);
                    wwip=data.host;
                }
            );
        });
    });

例2,js获取客户端ip地址及mac地址(转)
 

复制代码 代码示例:
<%@ page contenttype="text/html; charset=utf-8"%> 
<html>
<head>
<title>wmi scripting html</title> 
<meta http-equiv=content-type content="text/html; charset=gb2312"> 
<script type="text/javascript" src="getipandmac.js"></script> 
<script language=jscript 
    event="oncompleted(hresult,perrorobject, pasynccontext)" for=foo> 
    document.forms[0].txtmacaddr.value = unescape(macaddr); 
    document.forms[0].txtipaddr.value = unescape(ipaddr); 
    document.forms[0].txtdnsname.value = unescape(sdnsname); 
    //document.formbar.submit(); 
</script> 
<script language=jscript event=onobjectready(objobject,objasynccontext) 
    for=foo> 
    if (objobject.ipenabled != null && objobject.ipenabled != "undefined" 
            && objobject.ipenabled == true) { 
        if (objobject.macaddress != null && objobject.macaddress != "undefined") 
            macaddr = objobject.macaddress; 
        if (objobject.ipenabled && objobject.ipaddress(0) != null 
                && objobject.ipaddress(0) != "undefined") 
            ipaddr = objobject.ipaddress(0); 
        if (objobject.dnshostname != null 
                && objobject.dnshostname != "undefined") 
            sdnsname = objobject.dnshostname; 
    } 
</script>
</head> 
<body> 
<object id=locator classid=clsid:76a64158-cb41-11d1-8b02-00600806d9b6 viewastext></object> 
<object id=foo classid=clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223></object> 
<script language=jscript> 
    var service = locator.connectserver(); 
    var macaddr; 
    var ipaddr; 
    var domainaddr; 
    var sdnsname; 
    service.security_.impersonationlevel = 3; 
    service.instancesofasync(foo, 'win32_networkadapterconfiguration'); 
</script> 
<form id=formfoo name=formbar action=macjsp.jsp method=post><input 
    value="" name="txtmacaddr"> <input value="" name="txtipaddr"> 
<input value="" name="txtdnsname" onclick="getipandmacaddress()"></form> 
</body> 
</html>
 

Js获取客户端IP地址与MAC地址
JS获取客户端IP地址与机器名示例