Search code examples
javascriptinternet-explorergeolocation

IE 11 geolocation errorCallback doesn't work, when you click on [ X close ] in prompt.


if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successLocation, errorCallback, {timeout: 5000, enableHighAccuracy: false});
} else {
    errorCallback();
}
function successLocation() {
     console.log('success');
}
function errorCallback() {
    console.log('error');
}

In Chrome, when you click on [ X close ] errorCallback is works. PS: timeout doesn't work also in IE 11. enter image description here


Solution

  • I try to make a test with code below on several machines with IE 11.

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to get your coordinates.</p>
    
    <button onclick="getLocation()">Try It</button>
    
    <p id="demo"></p>
    
    <script>
    var x = document.getElementById("demo");
    
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition,errorCallback);
        } else { 
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    
    function showPosition(position) {
        x.innerHTML = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;
        successLocation();
    }
    function successLocation() {
         console.log('success');
    }
    function errorCallback() {
        console.log('error');
    }
    </script>
    
    </body>
    </html>

    All I find that, If you don't allow the access for a location (click on [X]) then IE will stop the execution at that point and will not execute any further code.

    As you know, If you allow the access for the location then also there can be any error occur. In that case IE will execute the error callback.

    You can see in testing results below.

    enter image description here

    enter image description here

    So as you can see in my testing results, Error callback is working in IE 11.