使用javascript获取本地计算机的ip地址
当我使用使用javascript获取本地计算机的ip地址,javascript,websocket,Javascript,Websocket,当我使用Request.UserHostAddress使用Javascript获取我的机器的ip地址时,我没有得到ip地址,而是得到了未定义的ip地址 我的代码: 您可以对hostip.info或类似服务执行ajax调用 function myIP() { if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else xmlhttp = new ActiveXObject("Microsoft.X
Request.UserHostAddress
使用Javascript获取我的机器的ip地址时,我没有得到ip地址,而是得到了未定义的ip地址
我的代码:
您可以对hostip.info或类似服务执行ajax调用
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;
}
您可以对hostip.info或类似服务执行ajax调用
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;
}
请尝试以下代码:
Ip地址:=
$(文档).ready(函数ubsrt()
{
window.rtpeerconnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=新的RTPeerConnection({iceServers:[]}),
noop=函数(){};
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
pc.onicecandidate=函数(ice){
如果(!ice | | |!ice.candidate | |!ice.candidate.candidate)返回;
var myIP=/([0-9]{1,3}(\[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
log('myIP:',myIP');
$('.ipAdd').text(myIP);
pc.onicecandidate=noop;
};
});
请尝试以下代码:
Ip地址:=
$(文档).ready(函数ubsrt()
{
window.rtpeerconnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=新的RTPeerConnection({iceServers:[]}),
noop=函数(){};
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
pc.onicecandidate=函数(ice){
如果(!ice | | |!ice.candidate | |!ice.candidate.candidate)返回;
var myIP=/([0-9]{1,3}(\[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
log('myIP:',myIP');
$('.ipAdd').text(myIP);
pc.onicecandidate=noop;
};
});
这是我的答案。我删除了jQuery的要求,将MechanisSM封装在一个函数中,并使用了一些ES6特性
<html>
<head><title>My IP Address</title></head>
<body><h3 class='ipAdd'>Ip Address : <h3></body>
<script> "use strict";
window.RTCPeerConnection = window.RTCPeerConnection ||
window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
function getMyIP (cb) {
// Calls the cb function with the local host IP address found
// using RTC functions. We cannot just return the IP address
// because the RTC functions are asynchronous.
var pc = new RTCPeerConnection ({iceServers: []}),
noop = () => {};
pc.onicecandidate = ice =>
cb = cb ((ice = ice && ice.candidate && ice.candidate.candidate)
? ice.match (/(\d{1,3}(\.\d{1,3}){3}|[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
: 'unavailable') || noop;
pc.createDataChannel ("");
pc.createOffer (pc.setLocalDescription.bind (pc), noop);
};
getMyIP (addr => { document.querySelector ('.ipAdd').innerHTML += addr; });
</script>
</html>
我的IP地址
Ip地址:
“严格使用”;
window.rtpeerconnection=window.rtpeerconnection||
window.mozRTPeerConnection||
window.webkirtpeerconnection;
函数getMyIP(cb){
//使用找到的本地主机IP地址调用cb函数
//使用RTC函数。我们不能只返回IP地址
//因为RTC功能是异步的。
var pc=新的RTPeerConnection({iceServers:[]}),
noop=()=>{};
pc.onicecandidate=ice=>
cb=cb((ice=ice&&ice.candidate&&ice.candidate.candidate)
?ice.match(/(\d{1,3}(\.\d{1,3}){3}{[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
:“不可用”)| | noop;
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
};
getMyIP(addr=>{document.querySelector('.ipAdd').innerHTML+=addr;});
这是我的答案。我删除了jQuery的要求,将MechanisSM封装在一个函数中,并使用了一些ES6特性
<html>
<head><title>My IP Address</title></head>
<body><h3 class='ipAdd'>Ip Address : <h3></body>
<script> "use strict";
window.RTCPeerConnection = window.RTCPeerConnection ||
window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
function getMyIP (cb) {
// Calls the cb function with the local host IP address found
// using RTC functions. We cannot just return the IP address
// because the RTC functions are asynchronous.
var pc = new RTCPeerConnection ({iceServers: []}),
noop = () => {};
pc.onicecandidate = ice =>
cb = cb ((ice = ice && ice.candidate && ice.candidate.candidate)
? ice.match (/(\d{1,3}(\.\d{1,3}){3}|[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
: 'unavailable') || noop;
pc.createDataChannel ("");
pc.createOffer (pc.setLocalDescription.bind (pc), noop);
};
getMyIP (addr => { document.querySelector ('.ipAdd').innerHTML += addr; });
</script>
</html>
我的IP地址
Ip地址:
“严格使用”;
window.rtpeerconnection=window.rtpeerconnection||
window.mozRTPeerConnection||
window.webkirtpeerconnection;
函数getMyIP(cb){
//使用找到的本地主机IP地址调用cb函数
//使用RTC函数。我们不能只返回IP地址
//因为RTC功能是异步的。
var pc=新的RTPeerConnection({iceServers:[]}),
noop=()=>{};
pc.onicecandidate=ice=>
cb=cb((ice=ice&&ice.candidate&&ice.candidate.candidate)
?ice.match(/(\d{1,3}(\.\d{1,3}){3}{[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
:“不可用”)| | noop;
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
};
getMyIP(addr=>{document.querySelector('.ipAdd').innerHTML+=addr;});
Ip地址:=
$(文档).ready(函数ubsrt()
{
window.rtpeerconnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=新的RTPeerConnection({iceServers:[]}),
noop=函数(){};
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
pc.onicecandidate=函数(ice){
如果(!ice | | |!ice.candidate | |!ice.candidate.candidate)返回;
var myIP=/([0-9]{1,3}(\[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
log('myIP:',myIP');
$('.ipAdd').text(myIP);
pc.onicecandidate=noop;
};
});
Ip地址:=
$(文档).ready(函数ubsrt()
{
window.rtpeerconnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=新的RTPeerConnection({iceServers:[]}),
noop=函数(){};
pc.createDataChannel(“”);
pc.createOffer(pc.setLocalDescription.bind(pc),noop);
pc.onicecandidate=函数(ice){
如果(!ice | | |!ice.candidate | |!ice.candidate.candidate)返回;
var myIP=/([0-9]{1,3}(\[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
log('myIP:',myIP');
$('.ipAdd').text(myIP);
pc.onicecandidate=noop;
};
});
它工作得非常好。你能告诉我没有jquery它是如何工作的吗?原始版本只使用了两个jquery调用。第一个$(document).ready
不是必需的,第二个$('.ipAdd').text(myIP)
I替换为DOM版本:document.querySelector('.ipAdd').innerHTML+=addr代码>理解你的解释。它工作得很好。你能告诉我没有jquery它是如何工作的吗?原始版本只使用了两个jquery调用。第一个$(文档)