navigator.geolocation.getCurrentPosition():如果用户输入“no”,则触发回调
navigator.geolocation.getCurrentPosition(): fire callback if user says "no"?
这是我的代码的近似:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
// success!
},
function(error) {
// error
},
{ timeout: 10000 }
);
} else {
// your browser/device doesn't support geolocation
}
当这段代码运行时,浏览器会正确地请求用户允许跟踪其物理位置。如果用户说"是",它就会正确运行第一个参数('success')指定的函数。
我不清楚的是当用户说"不"时会发生什么。在我目前的测试中(在Firefox 7中),如果用户说"no",什么也不会发生。我有点期待错误回调(第二个函数)运行,但它没有。我希望对用户的负面反应做出反应(通过删除触发此调用的链接)。
您需要在navigator.geolocation.getCurrentPosition()
的第二个回调中处理它,该回调处理诸如用户拒绝其位置(以及其他事情)之类的问题。
navigator.geolocation.getCurrentPosition(fn, function(errorCode) {
if (errorCode == 1) {
alert('Y U NO GIVE LOCATION?');
}
});
W3C规范。错误码列表。
对上一个回复的小更正
navigator.geolocation.getCurrentPosition(your_success_function, function(error) {
if (error.code == 1) {
alert('Y U NO GIVE LOCATION?');
}
});
相关文章:
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 测试Angular Service解决错误回调中的promise
- 如何将一个JavaScript函数回调为多个函数
- JavaScript回调函数
- 用于回调的javascript参数
- 将json回调数据转换为日期
- 承诺在非节点式回调上使用Bluebird
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 如何使用jqueryAJAX从页面中回调多个变量
- jquery Onclick函数带有导致双击的回调排序函数
- Javascript点击事件回调不起作用
- 一旦加载并渲染了角度引导typeahead,就使用回调
- 成功回调永远不会被JSONP请求调用
- navigator.geolocation.getCurrentPosition():如果用户输入“no”,则触发回调
- ' fetch ', work with mode ' no-cors '不会调用promise.然后回调