惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Security Latest
Security Latest
Spread Privacy
Spread Privacy
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
U
Unit 42
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Scott Helme
Scott Helme
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
爱范儿
爱范儿
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Latest news
Latest news
GbyAI
GbyAI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
Jina AI
Jina AI
AI
AI
NISL@THU
NISL@THU
I
Intezer
G
GRAHAM CLULEY
B
Blog
S
Secure Thoughts
IT之家
IT之家
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 前端架构师

Claude Code 源码架构分析 我不是狐狸,我是那Harness Engineering 小龙虾的自我养成之路 vue3+ts添加公共富文本组件 vue watch详解 微信授权没搞过? css3图片防止变形 公众号使用微信sdk的正确姿势 es6手写一个call 同学给我来个手写的new吧 this综合篇 this绑定番外篇之箭头函数绑定 vue中引入公用过滤器? this详解下 012天this详解上 011天之跨域资源共享CORS 010天JSON.stringify()详解 009天之跨浏览器的事件处理程序 使用XHR上传文件要不要了解一下?
web离线应用前提之离线检测
前端架构师 · 2020-03-22 · via 博客园 - 前端架构师

离线web应用就是,设备不能上网情况下仍可以运行的应用。

    开发离线应用需要几个步骤,首先确保知道设备能否上网,以便下一步执行正确的操作。然后应用还能访问一定的资源(图像,css,js),之后这样才能正常工作,最后必须有一块本地空间用于保存数据,无论能否上网都不影响读写。

离线检测:navigator.onLine属性,true表示能上网,false表示设备离线。

支持的浏览器 ie6+,Safari5+,chorme11+ 都已经支持

firefox3+ 和opera10.6+ 支持navigator.online属性,但你必须勾选,菜单项-》web 开发人员(设置)-》脱机工作 才能让浏览器正常工作。

由于存在兼容问题,单独使用navigator.online属性不能确定网络是否连通,即便如此,在请求发生错误的情况下。检测这个属性仍然管用。实例如下:

if(navigator.online){
    //正常工作
} else {
    //执行离线状态时的任务
}

除了navigator.online 属性之外,为了更好的确定网络是否可用,html5还定义了两个事件:online 和offline,当网络从离线变为在线时,或从在线变为离线时候触发:

EventUtil.addHandler(window,'online',function() {
    alert('online')
})

EventUtil.addHandler(window,'offline',function() {
    alert('offline')
})

扫码加群,每日更新一篇前端技术文章一起成长。