gpt4 book ai didi

javascript - Firefox和Chrome用户关闭 “Physical Location”提示如何处理?

转载 作者:可可西里 更新时间:2023-11-01 02:58:17 24 4
gpt4 key购买 nike

我正在使用 Geolocation 对象和 getCurrentPosition()。

你有没有看到每次使用 getCurrentPosition 时 Firefox 和 Chrome 都会提示这些消息?

Chrome :

Example.com wants to track your physical location [allow] [deny] [ X close ]

火狐:

Example.com wants to know your location [Share Location] [Don't Share] [Close]

我的问题是:当用户不“允许”或“拒绝”位置选项而是关闭提示时如何处理?

我有这个 JAVASCRIPT 代码,但是当用户关闭提示时它不起作用:

            if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
onSuccess,
onError,
{
//enableHighAccuracy: true,
timeout: 5000
//maximumAge: 120000
}
);
alert("holaaaa");
} else {
alert('Geolocation not supported.');
}

我遇到了一些问题,有什么帮助吗?

This question is related with this post: How to call function when user Allows or Denies access to "Physical Location"?

最佳答案

spec如果用户简单地拒绝许可请求(而不是单击“拒绝”按钮),是否必须执行回调实际上非常安静:

For both getCurrentPosition and watchPosition, the implementation must never invoke the successCallback without having first obtained permission from the user to share location. Furthermore, the implementation should always obtain the user's permission to share location before executing any of the getCurrentPosition or watchPosition steps described above. If the user grants permission, the appropriate callback must be invoked as described above. If the user denies permission, the errorCallback (if present) must be invoked with code PERMISSION_DENIED, irrespective of any other errors encountered in the above steps. The time that is spent obtaining the user permission must not be included in the period covered by the timeout attribute of the PositionOptions parameter. The timeout attribute must only apply to the location acquisition operation.

我想说,从 getCurrentPosition 的 Angular 来看,驳回许可请求等同于拒绝请求。单击拒绝是显式的,而解雇(单击 X)是隐式的。

在功能上,不同之处在于,单击“拒绝”按钮意味着 (1) 当前请求被拒绝,以及 (2) 该站点被列入黑名单,并且永远不会提示用户再次共享该站点的位置;简单地取消请求只意味着当前请求被拒绝—— future 的请求(在未来 BROWSINGCONTEXT s——页面重新加载后)将再次提示。

在我看来,Chrome、Firefox 和 IE 当前的行为是一个错误——如果用户通过单击 X 取消权限请求,这三者都不会执行任何操作。如果地理定位,则应调用 errorCallback权限请求被驳回,因为在当前 BROWSINGCONTEXT 中对 getCurrentLocation 的任何进一步调用都将默默地失败。 (Opera 11.52 表现“正确”,因为它的权限请求 UI 只有允许/拒绝按钮——没有 X。)

Mozilla 有一个 bug他们说当前行为是正确的,更新的 bug打开相关的错误; Chromuim also says当前行为是正确的。 IE 有 a bug (需要注册),但奇怪的是,Microsoft 将其关闭为不可复制。

这确实是 W3C 工作组需要解决的问题。该页面应该能够对用户拒绝位置权限请求使用react——无论是永久拒绝还是只是通过单击 X 拒绝此 session 。


要解决这种情况,您现在唯一可以做的就是设置您自己的超时时间。在地理定位请求中设置一个超时时间,然后将您的错误超时时间设置得比该时间更长(以允许用户有时间对请求使用react):

if (navigator.geolocation) {
var etimeout = setTimout(onError, 10000);
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout:5000});
}
function onSuccess(pos) {
clearTimeout(etimeout);
// ...
}
function onError(err) {
clearTimeout(etimeout);
// Note `err` will actually be undefined if this is the result of our timeout
// and anything you do here should be undone by `onSuccess` (in case the user
// took longer than 5 seconds to approve the request; for that reason, you
// shouldn't display any modal UI from here.

// ...
}

关于javascript - Firefox和Chrome用户关闭 “Physical Location”提示如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9219540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com