所以我有这段 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,或者因为它成功启动了异步请求 - 似乎没有办法区分这两者。
关于javascript - 如果用户拒绝在 Firefox 中共享地理位置,则永远不会调用函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947637/