jq获取本地网络ipv4地址(与网络属性中相同)

时间:2025-01-15 07:00:38
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <script src="js/jquery-3.4."></script> <script type="text/javascript"> function local_ip(){ var mytimeout; if ( window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection ){ mytimeout = setTimeout(function(){$("#list").html("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; var 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; $("#list").html(ip); }; } else{ $("#list").html("未获取到"); } } local_ip(); </script> <body> IP :<div id=list></div> </body> </html>