关于js获取内外网ip地址
小记这里有篇讲WebRTC获取内网IP的方法https://www.jianshu.com/p/8d7348bc84f4WebRTC的官方说明:https://developer.mozilla.org/zh-CN/docs/Web/API/WebRTC_APIfunction local_ip(){var $mytimeout;if ( window.RTCPeerConnection || w
·
小记
这里有篇讲WebRTC获取内网IP的方法
https://www.jianshu.com/p/8d7348bc84f4
不同浏览器开启WebRTC的方法:
https://www.yu1u.org/1Iibb_VYx/
WebRTC的官方说明:
https://developer.mozilla.org/zh-CN/docs/Web/API/WebRTC_API
这里一篇:
https://blog.csdn.net/u011118482/article/details/104848709/
function local_ip(){
var $mytimeout;
if ( window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection ){
$mytimeout = setTimeout(function(){document.getElementById('list').innerHTML ="Local IP address is not supported in this browser";},3000);
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var $pc = new RTCPeerConnection({iceServers:[]}),
$noop = function(){};
$pc.createDataChannel("");
$pc.createOffer($pc.setLocalDescription.bind($pc), $noop);
$pc.onicecandidate = function($ice){
clearTimeout($mytimeout);
if(!$ice || !$ice.candidate || !$ice.candidate.candidate) return;
console.log($ice.candidate);
$ip = /([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];
$pc.onicecandidate = $noop;
document.getElementById('list').innerHTML = $ip;
};
}
else{
document.getElementById('list').innerHTML = "-";
}
}
html:
IP is: <h1 id=list>-</h1>
此方法会将本地ip显示到h1标签中
使用谷歌浏览器,按照上述博客进行设置
效果:
如果是火狐浏览器:按照下面方式操作:
再就是通过搜狐、腾讯的接口打印外网ip
js使用cookie
https://www.cnblogs.com/YanSmallKind/p/11274850.html
笔记
更多推荐
已为社区贡献1条内容
所有评论(0)