w3c中html的绝对位置,html-被W3C地理位置示例代码所迷惑-如果找不到位置,它将无限循环吗?...

本文讨论了W3C地理位置规范中示例代码可能带来的安全性问题,特别关注了强制用户代理返回最新位置时,当缓存失效且持续超时的情况。代码中关于最大年龄和超时设置可能导致无限循环,作者深入解析了如何避免和处理这种困境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有点担心W3C Geolocation spec中此代码示例的安全性:

// Forcing the user agent to return a fresh cached position.

// Request a position. We only accept cached positions whose age is not

// greater than 10 minutes. If the user agent does not have a fresh

// enough cached position object, it will immediately invoke the error

// callback.

navigator.geolocation.getCurrentPosition(successCallback,

errorCallback,

{maximumAge:600000, timeout:0});

function successCallback(position) {

// By using the 'maximumAge' option above, the position

// object is guaranteed to be at most 10 minutes old.

// By using a 'timeout' of 0 milliseconds, if there is

// no suitable cached position available, the user agent

// will aynchronously invoke the error callback with code

// TIMEOUT and will not initiate a new position

// acquisition process.

}

function errorCallback(error) {

switch(error.code) {

case error.TIMEOUT:

// Quick fallback when no suitable cached position exists.

doFallback();

// Acquire a new position object.

navigator.geolocation.getCurrentPosition(successCallback, errorCallback);

break;

case ... // treat the other error cases.

};

}

function doFallback() {

// No fresh enough cached position available.

// Fallback to a default position.

}

如果浏览器确实由于某种原因无法返回位置修复,并且持续超时,会发生什么情况?

当然,代码将最终陷入无限循环,并一遍又一遍地调用errorCallback.

我看到了doFallback()调用,但这不会阻止errorCallback被重复调用.还是会?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值