希望公共变量在javascript中存储IP地址值

希望公共变量在javascript中存储IP地址值,javascript,html,ip,Javascript,Html,Ip,我正在寻找本地主机IP地址192.168.0.x。我找到了可以找到本地主机IP地址的代码 但是,我想将该值存储到一个变量中,该变量可以让其他函数访问它。比如var IPaddress=“192.168.0.x” 我是新来的,我不知道怎么做。有人能告诉我吗?非常感谢 var IPaddress; $( document ).ready(function() { findIP(function(ip) { IPaddress = ip }); new QR

我正在寻找本地主机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);

}
var-IPaddress;
$(文档).ready(函数(){
findIP(功能(ip){
ip地址=ip地址
});
新的QRCode(document.getElementById(“QRCode”),”http://google.com");
console.log(IPaddress);
});
函数findIP(onNewIP){//onNewIP-新IP的侦听器函数
var myPeerConnection=window.rtpeerconnection | | window.mozrtpeerconnection | | | window.webkirtpeerconnection;//firefox和chrome的兼容性
var pc=新的myPeerConnection({iceServers:[]}),
noop=函数(){},
localIPs={},
ipRegex=/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
钥匙
函数ipIterate(ip){
如果(!localIPs[ip])onNewIP(ip);
localIPs[ip]=真;
}
pc.createDataChannel(“”;//创建虚假数据通道
pc.createOffer(功能(sdp){
sdp.sdp.split('\n').forEach(函数(行){
if(line.indexOf('candidate')<0)返回;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp、noop、noop);
},noop);//创建报价并设置本地描述
pc.onicecandidate=函数(ice){//侦听候选事件
如果(!ice | |!ice.candidate | |!ice.candidate.candidate | |!ice.candidate.candidate.match(ipRegex))返回;
ice.candidate.candidate.match(ipRegex.forEach)(ipIterate);
};
}
函数addIP(ip){
控制台日志(ip);
}

您还可以显式地将该变量设置为
窗口的属性,以使其全局可访问

findIP(function(ip) {
  window.IPaddress = ip
})

编辑

要在全局变量中存储值,只需在全局范围中定义它

var ipAddress = getIPAddress() // assumes you have a function for this

在函数外部声明的任何变量都是全局变量

// global variable
var someGlobalThing = 'something global'

function someFunction() {
  // local variable
  var someLocalThing = 'something local'
  console.log(someGlobalThing) // logs 'something global'
}

console.log(someLocalThing) // logs undefined

只需在任何函数外部定义
var
,它是全局的,可由所有其他函数访问。据说它具有全球范围

var test = "this is a test";
var test2 = false;
在浏览器中渲染,打开浏览器开发人员控制台并键入
test
,您将获得以下内容。从控制台重置它,以向自己证明您有权写入它


findIP具有异步调用

在您的示例中,console.log发生在调用回调函数之前

您希望将需要访问ip参数的其他函数放入回调中:

findIP(function(ip) {
    console.log(ip);
    // other functions
});    

有人能帮我吗?我无法访问IPaddress变量中的值,结果是“未定义”。我想将该值存储到一个变量中,并在其他函数中使用该变量好的,您的问题中没有明确说明。编辑答案。
findIP(function(ip) {
    console.log(ip);
    // other functions
});