javascript - 如果用户拒绝在 Firefox 中共享地理位置,则永远不会调用函数失败

标签 javascript firefox callback geolocation

所以我有这段 javascript 代码。在 safari 和 chrome 中,如果用户拒绝共享位置,它将无法正常运行;然而,在 Firefox 中,它没有。

任何帮助表示赞赏。

function initGeolocation()
{
    if( navigator.geolocation )
    {          
          // Call getCurrentPosition with success and failure callbacks
          navigator.geolocation.getCurrentPosition( success, fail );
    }
    else
    {
          alert("Sorry, your browser does not support geolocation services.");
    }
}

 var map;
 function success(position)
 {

     var longIDText = document.getElementById('longID');
     var latIDText = document.getElementById('latID');
     longIDText.value = position.coords.longitude;
     latIDText.value = position.coords.latitude;
     document.getElementById('coordSubmitID').click();
  }

  function fail(error)
  {
          alert("FAAAAAAAAAAIIIIIIIIIL")
          var zip_code ;
          while (true){
              // Could not obtain location

              zip_code = prompt("Please enter your current address or zip code","");


              if ( zip_code == "" ) {
                  alert(zip_code +" is not a valid address. Please try again.");
              } 
              else{
                break;
              }
          }
          var zipIDText = document.getElementById('zipID');
          zipIDText.value = zip_code;
          document.getElementById('coordSubmitID').click();
  }

最佳答案

对于 Firefox 它 seems that PERMISSION_DENIED 如果选择了“从不共享”,则引发;如果关闭对话框或选择“以后再说”,实际上什么也不会发生——即使是在 mozillas geolocation demo 上如果您关闭权限 UI,则什么也不会发生。

这意味着 getCurrentPosition 可以返回,因为用户关闭了确认 UI,或者因为它成功启动了异步请求 - 似乎没有办法区分这两者。

https://bugzilla.mozilla.org/show_bug.cgi?id=675533

关于javascript - 如果用户拒绝在 Firefox 中共享地理位置,则永远不会调用函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947637/

相关文章:

javascript - 使用 module.exports 和 ES6 导出导入

css - Firefox 转换不工作。这是一个错误吗?

javascript - 确定元素是否由 JS 与原始 HTML 文档添加*或*检测脚本何时由 InnerHtml 更新节点

javascript - 如何降低根据数据类型回调的函数的复杂性?

c++ - 如何在 winapi 标准对话框中处理键盘事件?

javascript - 在这些代码中,如何使用执行上下文解释输出结果?

javascript - Chrome : Why change style in batch(cssText) is slower then change in single(. style.property) 方式?

javascript - Nodejs 请求返回行为不当

python - 在 Ubuntu 中从 Python 更改浏览器代理设置?

javascript - 使用 Promise.all 等待所有回调完成