<!doctype html>
<head
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
IP is: <h1 id=list>-</h1>
<script>
local_ip();
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;
$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 = "-";
}
}
</script>
</body>
</html>
浏览器获取本地IP
最新推荐文章于 2026-04-03 09:38:16 发布
本文介绍了一种使用JavaScript和WebRTC API获取本地IP地址的方法。通过创建RTCPeerConnection实例并触发ICE候选收集过程,可以捕获本地IP信息,即使在不支持WebRTC的浏览器中也能显示未受支持的消息。
1313

被折叠的 条评论
为什么被折叠?



