【发布时间】:2017-06-14 12:25:33
【问题描述】:
我正在寻找本地主机 IP 地址 192.168.0.x 。我找到了可以找到本地主机IP地址的代码。
但是,我想将值存储到一个变量中,该变量可以让其他函数访问它。像 var IPaddress = "192.168.0.x";
我是新手,我不知道该怎么做。谁能告诉我?非常感谢
var IPaddress;
$( document ).ready(function() {
findIP(function(ip) {
IPaddress = ip
});
new QRCode(document.getElementById("qrcode"), "http://google.com");
console.log(IPaddress);
});
function findIP(onNewIP) { // onNewIp - your listener function for new IPs
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new myPeerConnection({iceServers: []}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function ipIterate(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice) { //listen for candidate events
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
};
}
function addIP(ip) {
console.log(ip);
}
【问题讨论】:
-
谁能帮帮我?
标签: javascript html ip