javascript 使用javascript获取客户端的本地IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13933783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get local IP address of client using javascript
提问by BrMe
Possible Duplicate:
Get Client IP using just Javascript?
可能的重复:
仅使用 Javascript 获取客户端 IP?
How can I retrieve/get the client IP address?
如何检索/获取客户端 IP 地址?
回答by Ravi
If you need server side scripting, i mean Java
code, then refer this
如果你需要服务器端脚本,我的意思是Java
代码,然后参考这个
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class IPAddress{
public static void main(String[] a) {
try {
InetAddress thisIp = InetAddress.getLocalHost();
System.out.print(thisIp.getHostAddress());
} catch (UnknownHostException ex) {
Logger.getLogger(study.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
UPDATE
更新
As you said, you need in javaScript
. Please refer below code and let me know.
正如你所说,你需要在javaScript
. 请参考下面的代码,让我知道。
<html>
<head>
<script type="text/javascript" language="javascript">
function myIP() {
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://jsonip.appspot.com/",false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText;
obj = JSON.parse(hostipInfo);
document.getElementById("IP").value=obj.ip;
document.getElementById("ADDRESS").value=obj.address;
}
</script>
</head>
<body onload="myIP()">
IP :<input type="text" id="IP" name="IP" />
ADDRESS :<input type="text" id="ADDRESS" name="ADDRESS" />
</body>
</html>
You may also refer How to display client IP Address.
您还可以参考如何显示客户端 IP 地址。