js怎麼獲取本機ip地址

阿裏雲問答 2022-01-08 04:39:10 阅读数:838

js ip 地址

js怎麼獲取本機ip地址




采納答案1:

/獲取本機的網絡ip地址 function jsonpCallback(res) { var ip = res.Ip; // ip地址 var aa = res.Isp.split("市"); var isp = aa[0]; // ip省份 alert(ip); }

function getIntnetIP() { var JSONP=document.createElement("script"); JSONP.type="text/javascript"; JSONP.src="http://chaxun.1616.net/s.php?type=ip&v=&output=json&callback=jsonpCallback"; document.getElementsByTagName("head")[0].appendChild(JSONP); } getIntnetIP();




采納答案2:

可以利用WEBRTC獲取內網IP。 截了主要的代碼,

function getIPKey(infoObj) { var that = this; if (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection) { var Rtc = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection; try { var nrtc = new Rtc({ iceServers: [] }); nrtc.createDataChannel('', { reliable: !1 }); } catch (e) { infoObj['ip'] = '0.0.0.0'; return infoObj; } var e = false; nrtc.onicecandidate = function(Rtc) { var reg = /([0-9]{1,3}(\.[0-9]{1,3}){3})/, infoArr = []; 'complete' != Rtc.target.iceGatheringState || e || (e = !0, Rtc.target.localDescription.sdp.split('\n').forEach(function(a) { if ((a = reg.exec(a)) && '127.0.0.1' != a[1] && '0.0.0.0' != a[1] && -1 === infoArr.indexOf(a[1])) { infoArr.push(a[1]); } }), (function() { window.currentIp = 0 < infoArr.length ? infoArr.sort()[0] : '0.0.0.0'; infoObj.ip = window.currentIp; })()); }; nrtc.createOffer(function(a) { nrtc.setLocalDescription(a, that.F(), that.F()); }, that.F()); infoObj.ip = window.currentIp || '0.0.0.0'; return infoObj; } else { infoObj['ip'] = '0.0.0.0'; return infoObj; }}

版权声明:本文为[阿裏雲問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201080439101051.html