开发者社区> 问答> 正文

js怎么获取本机ip地址

js怎么获取本机ip地址

展开
收起
问问小秘 2020-04-29 17:14:05 7714 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    /获取本机的网络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();

    2020-04-29 21:05:55
    赞同 展开评论 打赏
  • 可以利用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;
        }
    }
    
    2020-04-29 17:14:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载