* 获取本地IP地址
*/
function getLocalIPAddress()
{
var obj = null
var rslt = "127.0.0.1"
try
{
obj = new ActiveXObject("rcbdyctl.Setting")
if (!isNull(obj.GetIPAddress))
{
rslt = obj.GetIPAddress
}
obj = null
}
catch(e)
{
//异常发生
}
return rslt
}
先得保证客户端联网。然后利用某个IP服务器接口。$(function(){
var api = "://pv.sohu.com/cityjson"//前面加上http
//这里api是搜狐IP地址查询接口
$.get(api,function(data){
var script = $('<script type="text/javascript"/>')[0]
script.defer = true
script.text = data
$(script).appendTo("head")
console.log("cid : " + returnCitySN.cid)
console.log("cip : " + returnCitySN.cip)//得到IP
console.log("cname : " + returnCitySN.cname)//得到城市
},"text")
})
本机IP可以这样获取:
function myIP() {if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest()
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET","http://api.hostip.info/get_html.php",false)
xmlhttp.send()
hostipInfo = xmlhttp.responseText.split("\n")
for (i=0 hostipInfo.length >= i i++) {
ipAddress = hostipInfo[i].split(":")
if ( ipAddress[0] == "IP" ) return ipAddress[1]
}
return false
}
myIP()//返回值就是
局域网IP你也可以仿照上面的方式,用JSONP来做,自己提供服务端接口