I still struggle with functions, variables and scope.
I've read several articles "explaining" the concepts, plus a number of seemingly related posts but cannot figure this one out. How can I access the vars ip/prov/country outside the function?
Here is working code (vars accessed inside the fn):
$.get("http://ipinfo.io", function (response) {
var ip, prov, country;
ip = response.ip;
prov = response.region;
country = response.country;
alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );
}, "jsonp");
However, I want to access the vars outside the function, to use later on.
This did not work on my site (all items displayed as "undefined"):
var ip, prov, country;
$.get("http://ipinfo.io", function (response) {
ip = response.ip;
prov = response.region;
country = response.country;
}, "jsonp");
alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );
However, it worked just fine in this jsFiddle !
Also, this did not work (all undefined):
var ip, prov, country;
$(function(){
$.get("http://ipinfo.io", function (response) {
ip = response.ip;
prov = response.region;
country = response.country;
}, "jsonp");
alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );
}); //END document.ready
What must I do on my site to access the ip/prov/country vars outside the $.get
function?