I understand that this question has already been asked before; however, I am still having trouble with this concept. As you can see, the first function works when setting 'profile' to a global variable, but when I try and mimic that same result with the second function, I run into trouble. I get a resultArray is not defined
error.
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(); // mandatory
document.write("Firstname is: " + profile[0]);
function pressme() {
$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
//Testing to see if response returns correctly
var response = (data);
console.log(response);
resultArray = [];
resultArray[0] = response.country_name;
console.log(resultArray[0]);
document.getElementById("demo").innerHTML = response.country_name;
});
}
pressme();
document.write("Country is: " + resultArray[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3> Returns Country of Origin from users IP Address</h3>
<p id="demo"></p>