1

I want to use a variable value that is created inside a function to outside of that function. but it is showing undefined. actually I am getting ip address using api and want to use that ip address outside the function. Inside function,its working fine but outside it alerts undefined. Here is my code

<script type="text/javascript">
var userip;
    window.onload = function () {
        var script = document.createElement("script");

        script.type = "text/javascript";

        script.src = "https://jsonip.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;     
        userip  = response.ip;
        alert(userip);  // alerts  ip address
    }
alert(userip); // alerts undefined. it should alert ip address

</script>
Newbyte
  • 2,421
  • 5
  • 22
  • 45
Rizwi
  • 117
  • 1
  • 13
  • You've to call the function in order to get the ip assigned to `userip`. – Teemu Mar 01 '19 at 19:07
  • 2
    this is a sync issue. the alert outside of the `onload` function runs before all the stuff inside the function – Scrimothy Mar 01 '19 at 19:07
  • 1
    If `DisplayIP` is called within the appended script, then Scrimothy's comment tells what's wrong. – Teemu Mar 01 '19 at 19:11

2 Answers2

1

In your code:

var userip  = response.ip;

Get rid of var making it:

userip  = response.ip;

Beware, this makes it to the global scope. And it will be subjected to be accessed and modified by all. So, make sure you give a unique name.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Your code are executed in the same order it was written. Your script begin to request "https://jsonip.com/?callback=DisplayIP" at on load page, but your last "alert" are executed following your code order and at this time "userip" has not a value. Its value is filled just when your request are finished. For this reason "alert" inside DisplayIP works and the last one not.

<script type="text/javascript">
    var userip;
    window.onload = function () {
        var script = document.createElement("script");

        script.type = "text/javascript";

        script.src = "https://jsonip.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };

    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;     
        userip  = response.ip;
        alert(userip);
    }

    alert(userip);

    // Try this to understanding what I mean
    setInterval(_ => alert(userip), 1000);
</script>
Marcelo Vismari
  • 1,147
  • 7
  • 15
  • you are right. so whats the solution? – Rizwi Mar 01 '19 at 19:17
  • Depend on what you intend. You can call some function after DisplayIP triggered. Could you provide some more details? Would you like to show this value in some input? – Marcelo Vismari Mar 01 '19 at 19:20
  • You can use variable userip in whole code since it's a global variable. But make sure execute the rest of your code after DisplayIP is triggered: function DisplayIP(response) { ... userip = response.ip; someFunction(); } – Marcelo Vismari Mar 01 '19 at 19:26
  • can you check same question here and answer please? everything is explained here in another question https://stackoverflow.com/questions/54956954/how-to-get-ip-in-variable-using-javascript – Rizwi Mar 02 '19 at 09:20