3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでブラウザの接続状態を取得する

Last updated at Posted at 2019-12-24

window.navigator.onLineプロパティ

ブラウザの接続状態を真偽値で表すプロパティ。

  • true: 接続されている
  • false: 接続されていない

ユーザがリンクをたどる、もしくは、スクリプトがリモートページを要求したときに値が更新される。
例えば、閲覧しているページからユーザがリンクをクリックした時に、インターネットへの接続が切れてていると、プロパティはfalse を返す。

ブラウザによる実装状況

Chrome, Safari

  • false: ブラウザがLANまたはルータに接続できないとき
  • true: 上記以外(必ずしも、インターネットにアクセスできる状態とは限らない)

Firefox, IE

  • false: ブラウザがオフラインモードにあるとき
  • true: 上記以外

*irefox 41以降では実際のネットワーク接続状態に従って値を返す

online / offline イベント

onLineプロパティの値更新に準じて、Windowに対しイベントが発生する。
onLineプロパティと同様、必ずしも、インターネットにアクセスできる状態であるかを示す値ではないので注意する。

//オンラインになった時
window.ononLine(function() {
//処理内容
)
//オフラインになった時
window.onoffLine(function() {
//処理内容
)

参考

3
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?