javascript - 当您在提示中单击 [ 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 中也不起作用。
解决方案
我尝试在几台装有 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 中有效。
推荐阅读
- javascript - 解构并分配给实例属性
- flutter - Flutter Run 无法解析配置“:classpath”的所有工件。找不到com.android.tools.build::4.1.0
- java - org.apache.juli.AsyncFileHandler 内存泄漏怎么办?
- python - 如何使用我已经安装在其他 IDE 中的 python 包
- json - 在 Python 中将数据结构转换为另一个数据结构
- ios - 画中画崩溃-iOS
- python - 使用 Pyinstaller 打包 Kivy 应用程序 - AttributeError
- websocket - 接收“400 Bad Request”的 WebSocket 请求调用到 sap 应用程序
- typescript - 从对象中提取子类型
- oracle - 解释计划在名称部分显示列名?