谷歌浏览器中的地理位置错误回调

Geolocation error callback in Google chrome

本文关键字:错误 回调 地理位置 谷歌浏览器      更新时间:2023-09-26

谷歌浏览器(19.0.1084.56 m)中有一个奇怪的错误(或功能?)。我正在尝试在我的页面上运行下一个 JavaScript 代码:

//...
function successCallback(position) {
    console.log('success');
}
function errorCallback(error) {
    console.log('error');
}
navigator.geolocation.getCurrentPosition(
    successCallback,
    errorCallback,
    {timeout: 1000});
//...

浏览器问我:"网站名称想要跟踪您的物理位置 [允许] [拒绝] ...了解更多 [X]"。

当我单击允许/拒绝按钮时,回调函数工作正常。但是当我点击[X]按钮时,没有任何反应:(有没有办法检测点击[X]?

PS 看起来火狐具有相同的功能 火狐 11 和地理位置拒绝回调

我相信

Chrome没有忽略选项的回调。

如果我是你,我会尝试实现超时回调。

示例代码:

    _callback = false;
function successCallback(position) {
    _callback = true;
    console.log('success');
}
function errorCallback(error) {
     _callback = true; 
    console.log('error');
}
setTimeout(function(){if(!_callback)console.log('ignored')}, 10000); //chanche the timeout according your needs
navigator.geolocation.getCurrentPosition(
    successCallback,
    errorCallback,
    {timeout: 1000});