Getting variable out of double function












0














There was already post with same question, but none of answers was doing what i suppose to do. So here is code example:



function findIP(onNewIP) {
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: [{
urls: "stun:stun.l.google.com:19302"
}]
}),
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("");

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);

pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
};
}


That code is JS function, and after is goes usage:



function addIP(ip) {
var li = document.createElement('li');
li.textContent = ip;
document.getElementById("IPLeak").appendChild(li);
}

findIP(addIP);


So what this usage is doing, is taking all 3 ip's(local,local ip6 and public ip4) and displaying it with li element.However, what i need, is only one ip address(public ip4), and it need to be stored into variable so i can process it further. In this case variable which need to be taken out it's ip variable in function addIP(ip).



I did found one code example which can get variable outside function:






<script>
function profileloader()
{
profile = ; // no "var" makes this global in scope
profile[0] = "Joe";
profile[1] = "Bloggs";
profile[2] = "images/joeb/pic.jpg";
profile[3] = "Web Site Manager";
}
profileloader();
document.write("Firstname is: " + profile[0]);
</script>





But when i try to implement , problem is how it works with single function while in code there is function executing function, so i was not able to get variable output. Any ideas?










share|improve this question





























    0














    There was already post with same question, but none of answers was doing what i suppose to do. So here is code example:



    function findIP(onNewIP) {
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    var pc = new myPeerConnection({
    iceServers: [{
    urls: "stun:stun.l.google.com:19302"
    }]
    }),
    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("");

    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);

    pc.onicecandidate = function(ice) {
    if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
    };
    }


    That code is JS function, and after is goes usage:



    function addIP(ip) {
    var li = document.createElement('li');
    li.textContent = ip;
    document.getElementById("IPLeak").appendChild(li);
    }

    findIP(addIP);


    So what this usage is doing, is taking all 3 ip's(local,local ip6 and public ip4) and displaying it with li element.However, what i need, is only one ip address(public ip4), and it need to be stored into variable so i can process it further. In this case variable which need to be taken out it's ip variable in function addIP(ip).



    I did found one code example which can get variable outside function:






    <script>
    function profileloader()
    {
    profile = ; // no "var" makes this global in scope
    profile[0] = "Joe";
    profile[1] = "Bloggs";
    profile[2] = "images/joeb/pic.jpg";
    profile[3] = "Web Site Manager";
    }
    profileloader();
    document.write("Firstname is: " + profile[0]);
    </script>





    But when i try to implement , problem is how it works with single function while in code there is function executing function, so i was not able to get variable output. Any ideas?










    share|improve this question



























      0












      0








      0







      There was already post with same question, but none of answers was doing what i suppose to do. So here is code example:



      function findIP(onNewIP) {
      var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
      var pc = new myPeerConnection({
      iceServers: [{
      urls: "stun:stun.l.google.com:19302"
      }]
      }),
      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("");

      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);

      pc.onicecandidate = function(ice) {
      if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
      ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
      };
      }


      That code is JS function, and after is goes usage:



      function addIP(ip) {
      var li = document.createElement('li');
      li.textContent = ip;
      document.getElementById("IPLeak").appendChild(li);
      }

      findIP(addIP);


      So what this usage is doing, is taking all 3 ip's(local,local ip6 and public ip4) and displaying it with li element.However, what i need, is only one ip address(public ip4), and it need to be stored into variable so i can process it further. In this case variable which need to be taken out it's ip variable in function addIP(ip).



      I did found one code example which can get variable outside function:






      <script>
      function profileloader()
      {
      profile = ; // no "var" makes this global in scope
      profile[0] = "Joe";
      profile[1] = "Bloggs";
      profile[2] = "images/joeb/pic.jpg";
      profile[3] = "Web Site Manager";
      }
      profileloader();
      document.write("Firstname is: " + profile[0]);
      </script>





      But when i try to implement , problem is how it works with single function while in code there is function executing function, so i was not able to get variable output. Any ideas?










      share|improve this question















      There was already post with same question, but none of answers was doing what i suppose to do. So here is code example:



      function findIP(onNewIP) {
      var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
      var pc = new myPeerConnection({
      iceServers: [{
      urls: "stun:stun.l.google.com:19302"
      }]
      }),
      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("");

      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);

      pc.onicecandidate = function(ice) {
      if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
      ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
      };
      }


      That code is JS function, and after is goes usage:



      function addIP(ip) {
      var li = document.createElement('li');
      li.textContent = ip;
      document.getElementById("IPLeak").appendChild(li);
      }

      findIP(addIP);


      So what this usage is doing, is taking all 3 ip's(local,local ip6 and public ip4) and displaying it with li element.However, what i need, is only one ip address(public ip4), and it need to be stored into variable so i can process it further. In this case variable which need to be taken out it's ip variable in function addIP(ip).



      I did found one code example which can get variable outside function:






      <script>
      function profileloader()
      {
      profile = ; // no "var" makes this global in scope
      profile[0] = "Joe";
      profile[1] = "Bloggs";
      profile[2] = "images/joeb/pic.jpg";
      profile[3] = "Web Site Manager";
      }
      profileloader();
      document.write("Firstname is: " + profile[0]);
      </script>





      But when i try to implement , problem is how it works with single function while in code there is function executing function, so i was not able to get variable output. Any ideas?






      <script>
      function profileloader()
      {
      profile = ; // no "var" makes this global in scope
      profile[0] = "Joe";
      profile[1] = "Bloggs";
      profile[2] = "images/joeb/pic.jpg";
      profile[3] = "Web Site Manager";
      }
      profileloader();
      document.write("Firstname is: " + profile[0]);
      </script>





      <script>
      function profileloader()
      {
      profile = ; // no "var" makes this global in scope
      profile[0] = "Joe";
      profile[1] = "Bloggs";
      profile[2] = "images/joeb/pic.jpg";
      profile[3] = "Web Site Manager";
      }
      profileloader();
      document.write("Firstname is: " + profile[0]);
      </script>






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 13:10









      Matt Morgan

      2,2052820




      2,2052820










      asked Nov 23 '18 at 12:48









      Luke Ventura

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          you need to learn about callbacks https://developer.mozilla.org/en-US/docs/Glossary/Callback_function



          you function findIP takes a callback that function gets called a couple of times with an ip, so to log for example all ips you could call



          findIP(function(ip){console.log(ip)});





          share|improve this answer





















          • I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
            – Luke Ventura
            Nov 23 '18 at 17:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447033%2fgetting-variable-out-of-double-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          you need to learn about callbacks https://developer.mozilla.org/en-US/docs/Glossary/Callback_function



          you function findIP takes a callback that function gets called a couple of times with an ip, so to log for example all ips you could call



          findIP(function(ip){console.log(ip)});





          share|improve this answer





















          • I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
            – Luke Ventura
            Nov 23 '18 at 17:55
















          0














          you need to learn about callbacks https://developer.mozilla.org/en-US/docs/Glossary/Callback_function



          you function findIP takes a callback that function gets called a couple of times with an ip, so to log for example all ips you could call



          findIP(function(ip){console.log(ip)});





          share|improve this answer





















          • I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
            – Luke Ventura
            Nov 23 '18 at 17:55














          0












          0








          0






          you need to learn about callbacks https://developer.mozilla.org/en-US/docs/Glossary/Callback_function



          you function findIP takes a callback that function gets called a couple of times with an ip, so to log for example all ips you could call



          findIP(function(ip){console.log(ip)});





          share|improve this answer












          you need to learn about callbacks https://developer.mozilla.org/en-US/docs/Glossary/Callback_function



          you function findIP takes a callback that function gets called a couple of times with an ip, so to log for example all ips you could call



          findIP(function(ip){console.log(ip)});






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 13:07









          L.A

          10318




          10318












          • I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
            – Luke Ventura
            Nov 23 '18 at 17:55


















          • I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
            – Luke Ventura
            Nov 23 '18 at 17:55
















          I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
          – Luke Ventura
          Nov 23 '18 at 17:55




          I see that command is working, but i still dont get why not using something like, findIP(function(ip){var x="(ip)"}); it's not working. I read about that callback function but not sure how exactly to make it work.
          – Luke Ventura
          Nov 23 '18 at 17:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447033%2fgetting-variable-out-of-double-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)