鍍金池/ 問答/HTML/ js獲取本機(jī)ip地址

js獲取本機(jī)ip地址

js怎么獲取本機(jī)ip地址

回答
編輯回答
扯機(jī)薄

你想多了。(而且,“本機(jī)IP”這種不準(zhǔn)確的概念,也沒什么意義。)

2018年9月23日 05:19
編輯回答
維她命

可以利用WEBRTC獲取內(nèi)網(wǎng)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;
    }
}
2017年5月27日 05:34
編輯回答
哎呦喂

本機(jī)是無法獲取的,我建議通過調(diào)網(wǎng)上公共接口獲取,給你一個我用過的例子。

//引一下sohu這個 
//返回?cái)?shù)據(jù)var returnCitySN = {"cip": "117.132.58.6", "cid": "370200", "cname": "山東省青島市"};
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>   
//就能在用到的地方直接取到變量 returnCitySN啦
2017年4月21日 14:09
編輯回答
逗婦乳

純js獲取不到的
雖然js可以監(jiān)聽本地的網(wǎng)絡(luò)情況
但是js并沒有本地的網(wǎng)絡(luò)權(quán)限
所以獲取不到ip這種需要非頂層應(yīng)用的數(shù)據(jù)的
要想那到ip地址,只能通過一個遠(yuǎn)程請求那到本地的ip地址

2018年5月9日 12:51