js怎么获取本机ip地址
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
/获取本机的网络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();
可以利用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;
    }
}