测试是否已授予html5地理位置许可

Ric*_*kyA 5 javascript html5 geolocation

有谁知道是否有一种方法可以测试是否已授予先前的html5地理位置许可?

我尝试制作一个不会请求地理位置的脚本,除非已获得该页面的许可。不一定非要是html5;其他框架也可以。

小智 8

第一部分实际上至少在Chrome 43+和Firefox 46+上回答了您的问题(请参阅https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API):

navigator.permissions &&
navigator.permissions.query({name: 'geolocation'}).then(function(PermissionStatus) {
    if('granted' === PermissionStatus.state) {
        navigator.geolocation.getCurrentPosition(function(geoposition) {
            console.log(geoposition) /* You can use this position without prompting the user if the permission had already been granted */
        })
    }
})
Run Code Online (Sandbox Code Playgroud)


God*_*esZ 0

根据Geolocation API 规范,没有任何此功能。即使将其保存在浏览器首选项中的选项也不会推送到您的代码中。

如果它真的很重要并且您确实必须意识到这一点,那么您必须为每个常见的浏览器编写一个浏览器插件并要求用户安装它。但我不会怀疑这一点。