【问题标题】:Using javascript or jQuery, detect if a user denies browser's request to locate computer使用 javascript 或 jQuery 检测用户是否拒绝浏览器定位计算机的请求
【发布时间】:2013-10-15 13:24:21
【问题描述】:

我正在构建一个网站,该网站通过 - navigator.geolocation.getCurrentPosition(positionFound) 请求使用某人位置的权限。

使用javascript或jQuery,我如何检测用户是否拒绝权限,在他们拒绝之后的那一刻?

我的代码看起来像这样 -

function Geolocation() {
    navigator.geolocation.getCurrentPosition(positionFound)
}

function positionFound(position, found) {
    if (position.coords) {
    //do stuff
    }
}

部分问题是,在用户接受或拒绝使用计算机位置的权限之前,到达navigator 之后的代码。

【问题讨论】:

标签: javascript jquery geolocation


【解决方案1】:

navigator.geolocation.getCurrentPostions可以带两个参数success, error

将您的代码更改为:

function positionFound(position) {
    // dostuff
}

function positionNotFound(error) {
    // Handle error
}

function Geolocation() {
    navigator.geolocation.getCurrentPosition(positionFound, positionNotFound)
}

Source

【讨论】:

    【解决方案2】:

    Ian 是对的,看看这个example 使用getCurrentPosition

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2013-05-06
      • 1970-01-01
      • 2014-10-12
      • 2018-01-10
      • 2018-11-04
      • 2011-09-14
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多