Home  >  Article  >  Web Front-end  >  A simple example of getting the client's external network IP using js_javascript skills

A simple example of getting the client's external network IP using js_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:022332browse
var wwip=""; 
  $(function(){ 
    $(document).ready( function() { 
      $.getJSON( "http://smart-ip.net/geoip-json?callback=?", 
        function(data){ 
          alert( data.host); 
          wwip=data.host; 
        } 
      ); 
    }); 
  }); 

I checked a lot of information on this issue, but nothing works. This one is easy to use.

Example, js obtains local and external IP addresses.

<script language="javascript" type="text/javascript">
//获取本地IP
function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
alert("ErrInfoIS:"+e)
}
return rslt;
}
document.write("你的IP是:" + GetLocalIPAddress());
</script>

The above is how to get the local IP address with js. Let’s take a look at the example of getting the external IP address with js.

<script language="javascript">
//获取外网IP
xml=new ActiveXObject("Microsoft.XMLHTTP");
xml.open("GET","http://city.ip138.com/city0.asp",false);
xml.send();
kk=xml.ResponseText;
i=kk.indexOf("[");
ie=kk.indexOf("]");
ip=kk.substring(i+1,ie);
document.write("<span style='color:red;font-size:12;' cursor='hand'>您的IP地址是:" + ip + "</span>");
</script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn