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>