首页 > 解决方案 > 当您在提示中单击 [ X close ] 时,IE 11 geolocation errorCallback 不起作用。

问题描述

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

在 Chrome 中,当您单击 [ X close ] 时,errorCallback 有效。PS:超时在 IE 11 中也不起作用。 在此处输入图像描述

标签: javascriptinternet-explorergeolocation

解决方案


我尝试在几台装有 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>

我发现,如果您不允许访问某个位置(单击 [X]),那么 IE 将在该点停止执行并且不会执行任何进一步的代码。

如您所知,如果您允许访问该位置,那么也可能会发生任何错误。在这种情况下,IE 将执行错误回调。

您可以在下面的测试结果中看到。

在此处输入图像描述

在此处输入图像描述

正如您在我的测试结果中看到的,错误回调在 IE 11 中有效。


推荐阅读