当前位置 : 首页 » 编程语言 » 正文

js获取客户端外网ip的简单实例

分类 : 编程语言 | 发布时间 : 2018-04-21 09:06:21 | 浏览 : 0
var wwip=""; 
  $(function(){ 
    $(document).ready( function() { 
      $.getJSON( "http://smart-ip.net/geoip-json?callback=?", 
        function(data){ 
          alert( data.host); 
          wwip=data.host; 
        } 
      ); 
    }); 
  }); 

这个问题查了很多资料,都不可以,这个还好用。

例子,js获取本地与外网IP地址。

<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>

以上是js获取本地ip地址的方法,下面来看js获取外网ip地址的例子。

<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>

相关阅读:

Remove duplicates from an array of objects in JavaScript

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

Check whether a string matches a regex in JS

jQuery event to trigger action when a div is made visible

Check if user is using IE with jQuery

Resize HTML5 canvas to fit window

How do I replace a character at a particular index in JavaScript?

JavaScript file upload size validation

Converting a string to a date in JavaScript

How to get the data-id attribute?