TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Heyoffline.js warns your users when their network becomes unreachable

109 点作者 wassago超过 12 年前

9 条评论

chrisacky超过 12 年前
Today I learnt something that I didn't know exist, so i'm actually really glad that you linked this project, it made me look for details on the spec/standard.<p>First of all, I looked through your code and couldn't understand how this was implemented. After I found it and researched, it's clear that it's essentially useless[1].<p>I'm not bashing on your project, I like it, but aside from being a cool gimmick, I can't think of any real world usages?<p>...Unless your application is LAN-based , offline-heavy, or your have pixies that run around your office pulling out your network cable regularly * chuckles *.<p>I'll explain, but please correct me if I am way off the mark.<p>The project connects to the network events<p><pre><code> this.events = { network: ['online', 'offline'] }; </code></pre> These events fire from observing the attribute<p><pre><code> window.navigator.onLine </code></pre> This attribute returns true, when the system is connected to a network, but returns false when it isn't.<p>The importance of this is that the attribute is inherently unreliable. A computer can be connected to a network without having Internet access. [2]<p>This is a barebones minimal example that doesn't have the features you added:<p><pre><code> &#60;!DOCTYPE HTML&#62; &#60;html&#62; &#60;head&#62; &#60;title&#62;Online status&#60;/title&#62; &#60;script&#62; function updateIndicator() { document.getElementById('indicator').textContent = navigator.onLine ? 'online' : 'offline'; } &#60;/script&#62; &#60;/head&#62; &#60;body onload="updateIndicator()" onoffline="updateIndicator()"&#62; &#60;p&#62;The network is: &#60;span id="indicator"&#62;(state unknown)&#60;/span&#62; &#60;/body&#62; &#60;/html&#62; </code></pre> You can see the fiddle here: <a href="http://jsfiddle.net/3rRWK/" rel="nofollow">http://jsfiddle.net/3rRWK/</a><p>If you disconnect yourself from your network you will see the fiddle update to "Offline", however, if you pull your phone-line from your router, it will still stay at "Online".<p><pre><code> [1] : It's inherently unreliable. A computer can be connected to a network without internet access. [2] : http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html</code></pre>
评论 #4818881 未加载
评论 #4819294 未加载
评论 #4819321 未加载
评论 #4818646 未加载
评论 #4818689 未加载
评论 #4819298 未加载
评论 #4818970 未加载
评论 #4818632 未加载
评论 #4820554 未加载
philfreo超过 12 年前
If you want to roll your own UI (something less obtrusive, perhaps), this can be done in only a few lines of code. From our Backbone.js app...<p><pre><code> (function() { var errorView = null; $(window).on('online', function() { if (errorView) errorView.close(); errorView = null; }); $(window).on('offline', function() { errorView = new ErrorView({ message: 'No internet connection.'}); errorView.render(); }); }());</code></pre>
jbrooksuk超过 12 年前
Looks good to me. We run an internal web app which relies on users being connected to a local server. This is perfect for my needs.
ubermensche超过 12 年前
This looks great - but that sub-headline is almost unreadable (too thin/too light) "Warn your users when their network etc." (Chrome OSX)
frozenport超过 12 年前
I press the red button in the latest version of Firefox and nothing happens. The cursor doesn't change when I hover over the button.
评论 #4821296 未加载
copypasteweb超过 12 年前
This should be a browser feature, extension or user script, not a website feature.
评论 #4821237 未加载
BaconJuice超过 12 年前
This is really cool. Thank you for sharing!
jamespo超过 12 年前
works in chrome, doesn't work in an admittedly old firefox or IE8
评论 #4818626 未加载
评论 #4819328 未加载
评论 #4818932 未加载
评论 #4818559 未加载
tunnuz超过 12 年前
Very cool!
评论 #4820343 未加载