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关闭]时,errorCallback可以正常工作。 PS:超时在IE 11中也不起作用.
我尝试使用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中有效。