View Full Version : undetected error handling in callback

09-14-2011, 04:07 PM
so, I should start by saying that I don't really understand callbacks (but I understand what they do), and here's the thing:

html5 browser geolocation has an error callback which works fine EXCEPT if the user is using FF and refuses to share their location. In IE and Chrome, this produces a "permission denied" error which you can then handle. But firefox just sits there because it never receives the error. Here's an example (http://code.google.com/intl/fr/apis/maps/documentation/javascript/examples/map-geolocation.html) - click on Not now in FF to see what I mean.

So the only solution I can see is to make some special case for FF and this is where my question comes in - can I set a condition within the callback that says something like if it wasn't successful, but you didn't get an error, do this...

I thought that if (!position&&!error) {useMaxMind("Firefox fallback"); } might do it, but it can't go in the error callback (I guess because that only fires if an error is detected) and it doesn't work in the main function (I guess because if that code runs it means the location has been found)...

anyway. Here's what I have for the moment, which just sets a timeout as soon as it loads. It would be nice to have something a bit more elegant, though.

thanks in advance...

function initialize() {
// try W3C standard approach
var geoTimeout = 10000;
var timeOuthandler = setTimeout("useMaxMind('Timeout')", geoTimeout);
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latLong = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
alert("geolocation successful");
showMap(latLong, 16);
}, function (error) {
switch (error.code) {
case 0:
message = "Something went wrong: " + error.message;
case 1:
message = "You denied permission to this page to retrieve a location.";
case 2:
message = "The browser was unable to determine a location: " + error.message;
case 3:
message = "The browser timed out before retrieving the location.";
} clearTimeout(timeOuthandler);
}, {timeout:geoTimeout} );
} else {
useMaxMind("No browser support");