-2

var newIp;

function getIP(json) {
    document.write("My public IP address is: " + json.ip); //<-- This works properly
    var newIp = json.ip; //<-- Trying to change newIp into a global variable.
    console.log(typeof json.ip); //<-- Returns "string"
  }
  console.log(newIp); //<-- outputs to undefined?? Why??

There is also a script that I included to the index.html "src="http://api.ipify.org?format=jsonp&callback=getIP" and I do get the local IP address "json.ip" to work properly.

I cannot figure out how to turn the "json.ip" into a global variable to use in other parts of my app. Can someone give me a real example of how to do this?

Cristien
  • 23
  • 1
  • 7
  • 2
    `newIp =` - i.e. don't use `var` (or `let` or `const`) - but this smells of not knowing how to handle asynchrony :p – Jaromanda X Apr 18 '20 at 10:09
  • use this => `window.newIp = json.ip` – Harkal Apr 18 '20 at 10:11
  • Jaromanda X please explain with a useful example. – Cristien Apr 18 '20 at 11:00
  • @Cristien If it is indeed to do with asynchronicity, (which is likely) then you should have a look at [this post](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – Ivar Apr 18 '20 at 11:04

3 Answers3

0
var newIp;
function getIP(json) {
    document.write("My public IP address is: " + json.ip); //<-- This works properly
    newIp = json.ip; //<-- Trying to change newIp into a global variable.
    console.log(typeof json.ip); //<-- Returns "string"
}
getIP({ip:"127.0.0.1"});
console.log(newIp);
dellink
  • 544
  • 3
  • 16
0

If you define a variable into a function, you can't access it outside of that function. This variable is scoped into only for that function.

function greetings() {
    let name = 'Robin';
    console.log(name); // Robin
}
console.log(name); // undefined

So if you want to access that name variable outside of greetings function, you need to declare it on the same scope. In this case on global scope like that.

let name;
function greetings() {
    name = 'Robin';
    console.log(name); // Robin
}
console.log(name); // Robin

OR if you want to use a variable all over the places of your app, you can define it into window object or declare a global variable like let DB = {}. Then declare your variable like that DB.ip = 'value';

So you can get the value of ip all over the places using DB.ip. You can do it with window object also.

Robin
  • 4,902
  • 2
  • 27
  • 43
-1

updated

you have this code

<script type="text/javascript">
  function getIP(json) {
    document.write("My public IP address is: " + json.ip);
    window.newIp = json.ip;
    console.log(typeof json.ip); //<-- Returns "string"
  }
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<script type="text/javascript">
  console.log(newIp);
</script>

now you can put this code in a file, lets say myScript.js

  function getIP(json) {
    document.write("My public IP address is: " + json.ip);
    window.newIp = json.ip;
    console.log(typeof json.ip); //<-- Returns "string"
  }

and now you need to include them in your html in this order

<script src="./myScript.js"></script> <!--let's say your myscript is lying with html file in same folder-->
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<script>
  console.log(newIp);
</script>

here order of the scrip tag matters brother so try this and let me know

Harkal
  • 1,770
  • 12
  • 28
  • I removed the initial var newIp and it throws this error "api.js:20 Uncaught ReferenceError: newIp is not defined" which is the last console.log – Cristien Apr 18 '20 at 10:22
  • you need to run the function before last console.log – Harkal Apr 18 '20 at 10:23
  • i have updated the code look again – Harkal Apr 18 '20 at 10:24
  • Could you please explain how to set "json.ip" as a value in the getIP invocation (instead of a test ip) so I can set it as a variable and use it somewhere else? – Cristien Apr 18 '20 at 10:55
  • where do you invoke this function in your code? before me giving this code what you were doing with this code ? how did you use it in your code show me that part. what is the value of argument you pass in ? – Harkal Apr 18 '20 at 11:06
  • I included this to the index.html . I am trying to turn the Ip address into a variable so I can use it elsewhere in my application but I cannot figure out how to extract it from my function. json.ip gives me my ip address. I want to turn json.ip into a global variable. – Cristien Apr 18 '20 at 11:10
  • ok i got it wait for some time i m doing it. wait for some time – Harkal Apr 18 '20 at 11:13
  • Sorry could not get it to work for me. – Cristien Apr 18 '20 at 13:20
  • its working. i have successfully tested the code. how you are implementing it ? – Harkal Apr 18 '20 at 14:21
  • Can you tell me why when I move the function declaration into the JS file it breaks, but when it is is in the HTML file it works? https://codepen.io/ancri-gonzalez-torres/pen/WNQxRPR?editors=0001 – Cristien Apr 18 '20 at 18:35
  • look the updated answer and update your question with the approach you are doing ? i mean how you are puting them in different file ? – Harkal Apr 19 '20 at 00:30
  • I'm trying to keep proper separation of concerns. I'm trying to keep my JS in a seperate file. I'd like to keep the index.html clean without many scripts. See what I'm trying to do here. I know it has something to do with the execution order because when I move the function declaration and console.log over to the index.html it works fine. https://codepen.io/CristienL/pen/oNjLaGZ?editors=1111 – Cristien Apr 19 '20 at 11:00
  • if you really like coding using better architecture techniques then you must learn a framework. a lot of work is being handled by them. its seamless to use them. you get package management out of the box. so i recommend you to learn angular. you ll get addicted. – Harkal Apr 19 '20 at 12:04