after few days to break my mind and trying to solve my issue myself, I've decided to ask some help. Here is my code :
// -----------
// Global object
// -----------
window.myObj = window.myObj || {};
function getLocation(myObj) {
// Location test
// -- Mobile connection test/fix
// -- First step => Ask on client IP
$.getJSON( "//freegeoip.net/json/?callback=?", function(data) {
console.warn('Fetching Accurate JSON data...');
if (data.city !== '') {
// Log output to console
console.info('Location found from client ip');
console.info(JSON.stringify(data, null, 2));
// myObj = data;
// myObj.toString = function(data) {
// return JSON.stringify(data, null, 2);
// }
} else {
// -- Second step => Ask on server IP using server name
$.getJSON( "//freegeoip.net/json/" + window.location.host + "?callback=?", function(data) {
// Log output to console
console.info('Location found from server ip');
console.info(JSON.stringify(data, null, 2));
// myObj = data;
// myObj.toString = function(data) {
// return JSON.stringify(data, null, 2);
// }
});
}
});
return myObj;
}
console.info(getLocation(myObj));
What I want, is to store the result of the JSONP callback to the outer object named myObj
.
I've tried to create the function as a closure this way:
window.myObj = (function () {
// Location test
// -- Mobile connection test/fix
// -- First step => Ask on client IP
$.getJSON( "//freegeoip.net/json/?callback=?", function(data) {
console.warn('Fetching Accurate JSON data...');
if (data.city !== '') {
// Log output to console
console.info('Location found from client ip');
return data;
} else {
// -- Second step => Ask on server IP using server name
$.getJSON( "//freegeoip.net/json/" + window.location.host + "?callback=?", function(data) {
// Log output to console
console.info('Location found from server ip');
return data;
});
}
});
}());
but it returns undefined
everytime... As you may see my javascript skills are not so good, as my English too.
I thank you all in advance for your help.