PDA

View Full Version : Off site AJAX exception(s)?!



jscheuer1
06-24-2016, 09:42 PM
I would have thought this code was impossible (cross domain security). Yet I found its basics on Stack Overflow and played with it to make something somewhat useful, and damned if it doesn't work:


<!DOCTYPE html>
<html>
<head>
<title>Current Time in lat/lng from lat/lng Using Google Maps API and jQuery</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">
function curTimefrmLatLng(lat, lng){
var ts = Math.round(new Date().getTime() / 1000);
var gmre = /[^\d]*$/;
$.ajax({
url:"https://maps.googleapis.com/maps/api/timezone/json?location="+lat+","+lng+"&timestamp="+ts.toString(10),
success: function(response){
if(response.timeZoneId != null){
window.console && console.log(response);
ts = ts + response.dstOffset + response.rawOffset;
gm = new Date(ts * 1000).toUTCString().replace(gmre, " " + response.timeZoneName);
$('#results').html('Current time in ' + response.timeZoneId + ' : ' + gm);
}
}
});
}
curTimefrmLatLng(34.0522, -118.2437); // running with LA's lat/lng
</script>
</body>
</html>

Demo:

http://jscheuer1.com/demos/tz.htm

Only impressive if you're not in the pst/pdt timezone.