Ciao a tutti!!
Sono un perfetto newbie di Javascript. Ho trovato uno script ed ho cercato di adattarlo alle mie esigenze. Lo script è il seguente:
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function geolocate(timezone, cityPrecision, objectVar) {
var api = (cityPrecision) ? "ip_query.php" : "ip_query_country.php";
var domain = 'ipinfodb.com';
var url = "http://" + domain + "/" + api + "?output=json" + ((timezone) ? "&timezone=false" : "&timezone=false" ) + "&callback=" + objectVar + ".setGeoCookie";
var geodata;
var callbackFunc;
var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
} else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
if (str === "") str = '""';
eval("var p=" + str + ";");
return p;
};
//Check if cookie already exist. If not, query IPInfoDB
this.checkcookie = function(callback) {
geolocationCookie = getCookie('geolocation');
callbackFunc = callback;
if (!geolocationCookie) {
getGeolocation();
} else {
geodata = JSON.parse(geolocationCookie);
callbackFunc();
}
}
//API callback function that sets the cookie with the serialized JSON answer
this.setGeoCookie = function(answer) {
if (answer['Status'] == 'OK') {
JSONString = JSON.stringify(answer);
setCookie('geolocation', JSONString, 365);
geodata = answer;
callbackFunc();
}
}
//Return a geolocation field
this.getField = function(field) {
try {
return geodata[field];
} catch(err) {}
}
//Request to IPInfoDB
function getGeolocation() {
try {
script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
} catch(err) {}
}
//Set the cookie
function setCookie(c_name, value, expire) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expire);
document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
}
//Get the cookie content
function getCookie(c_name) {
if (document.cookie.length > 0 ) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start != -1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end == -1) {
c_end=document.cookie.length;
}
return unescape(document.cookie.substring(c_start,c_end));
}
}
return '';
}
}
</script>
</head>
<body>
<script type="text/javascript">
var MonthsOfYear = new Array(12);
MonthsOfYear[0] = "Gennaio";
MonthsOfYear[1] = "Febbraio";
MonthsOfYear[2] = "Marzo";
MonthsOfYear[3] = "Aprile";
MonthsOfYear[4] = "Maggio";
MonthsOfYear[5] = "Giugno";
MonthsOfYear[6] = "Luglio";
MonthsOfYear[7] = "Agosto";
MonthsOfYear[8] = "Settembre";
MonthsOfYear[9] = "Ottobre";
MonthsOfYear[10] = "Novembre";
MonthsOfYear[11] = "Dicembre";
var visitorGeolocation = new geolocate(false, true, 'visitorGeolocation');
var callback = function(){
var cityName = visitorGeolocation.getField('City');
var Digital = new Date();
var mday = Digital.getDate();
var month = Digital.getMonth();
var year = Digital.getFullYear();
var hours = Digital.getHours();
var h = parseInt(hours);
if (h <= 9) hours = "0" + hours;
var minutes = Digital.getMinutes();
var m = parseInt(minutes);
if (m <= 9) minutes = "0" + minutes;
var myclock = cityName + ', ' + mday + ' ' + MonthsOfYear[month] + ' ' + year + '
' + 'ore' + ' ' + hours + '.' + minutes;
document.write(myclock + ' Festività');
};
visitorGeolocation.checkcookie(callback);
</script>
</body>
</html>
Il problema è che document.write() non stampa nulla in quella funzione di callback, mente se per esempio metto alert al posto di document.write, mi appare la scritta. Non capisco perché.