From 280c67f60ca8287860d62294e1984e6a39d8eab0 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Mon, 2 Aug 2021 00:35:48 +0900 Subject: orphaned/Web/API/NavigatorOnLine 以下を移動 (#1657) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * orphaned/Web/API/NavigatorOnLine 以下を移動 - Web/API/Navigator 以下へ移動 - 2021/07/22 時点の英語版に同期 - https://github.com/mdn/translated-content/pull/612 対策 * 用語の修正漏れを修正 * fix flaws --- files/ja/_redirects.txt | 13 +-- files/ja/_wikihistory.json | 56 ++++----- .../ja/orphaned/web/api/navigatoronline/index.html | 60 ---------- .../web/api/navigatoronline/online/index.html | 89 --------------- .../online_and_offline_events/index.html | 74 ------------ files/ja/web/api/navigator/online/index.html | 78 +++++++++++++ .../navigator/online_and_offline_events/index.html | 127 +++++++++++++++++++++ 7 files changed, 236 insertions(+), 261 deletions(-) delete mode 100644 files/ja/orphaned/web/api/navigatoronline/index.html delete mode 100644 files/ja/orphaned/web/api/navigatoronline/online/index.html delete mode 100644 files/ja/orphaned/web/api/navigatoronline/online_and_offline_events/index.html create mode 100644 files/ja/web/api/navigator/online/index.html create mode 100644 files/ja/web/api/navigator/online_and_offline_events/index.html (limited to 'files') diff --git a/files/ja/_redirects.txt b/files/ja/_redirects.txt index 1b4a256351..9331206b20 100644 --- a/files/ja/_redirects.txt +++ b/files/ja/_redirects.txt @@ -1594,7 +1594,7 @@ /ja/docs/DOM/window.navigator.language /ja/docs/orphaned/Web/API/NavigatorLanguage/language /ja/docs/DOM/window.navigator.mimeTypes /ja/docs/orphaned/Web/API/NavigatorPlugins/mimeTypes /ja/docs/DOM/window.navigator.mozIsLocallyAvailable /ja/docs/Web/API/Navigator/mozIsLocallyAvailable -/ja/docs/DOM/window.navigator.onLine /ja/docs/orphaned/Web/API/NavigatorOnLine/onLine +/ja/docs/DOM/window.navigator.onLine /ja/docs/Web/API/Navigator/onLine /ja/docs/DOM/window.navigator.oscpu /ja/docs/Web/API/Navigator/oscpu /ja/docs/DOM/window.navigator.platform /ja/docs/orphaned/Web/API/NavigatorID/platform /ja/docs/DOM/window.navigator.plugins /ja/docs/orphaned/Web/API/NavigatorPlugins/plugins @@ -1819,7 +1819,7 @@ /ja/docs/DOM:window.navigator.language /ja/docs/orphaned/Web/API/NavigatorLanguage/language /ja/docs/DOM:window.navigator.mimeTypes /ja/docs/orphaned/Web/API/NavigatorPlugins/mimeTypes /ja/docs/DOM:window.navigator.mozIsLocallyAvailable /ja/docs/Web/API/Navigator/mozIsLocallyAvailable -/ja/docs/DOM:window.navigator.onLine /ja/docs/orphaned/Web/API/NavigatorOnLine/onLine +/ja/docs/DOM:window.navigator.onLine /ja/docs/Web/API/Navigator/onLine /ja/docs/DOM:window.navigator.oscpu /ja/docs/Web/API/Navigator/oscpu /ja/docs/DOM:window.navigator.platform /ja/docs/orphaned/Web/API/NavigatorID/platform /ja/docs/DOM:window.navigator.plugins /ja/docs/orphaned/Web/API/NavigatorPlugins/plugins @@ -3222,7 +3222,7 @@ /ja/docs/Notification_object/title /ja/docs/Web/API/Notification/title /ja/docs/NsIDOMHTMLMediaElement /ja/docs/orphaned/NsIDOMHTMLMediaElement /ja/docs/Offline_resources_in_Firefox/Using_audio_and_video_in_Firefox /ja/docs/Learn/HTML/Multimedia_and_embedding/Video_and_audio_content -/ja/docs/Online_and_offline_events /ja/docs/orphaned/Web/API/NavigatorOnLine/Online_and_offline_events +/ja/docs/Online_and_offline_events /ja/docs/Web/API/Navigator/Online_and_offline_events /ja/docs/Other_JavaScript_tools /ja/docs/Tools /ja/docs/Parsing_and_serializing_XML /ja/docs/Web/Guide/Parsing_and_serializing_XML /ja/docs/Participating_in_the_Mozilla_project /ja/docs/orphaned/Participating_in_the_Mozilla_project @@ -3618,9 +3618,8 @@ /ja/docs/Web/API/NavigatorLanguage.languages /ja/docs/orphaned/Web/API/NavigatorLanguage/languages /ja/docs/Web/API/NavigatorLanguage/language /ja/docs/orphaned/Web/API/NavigatorLanguage/language /ja/docs/Web/API/NavigatorLanguage/languages /ja/docs/orphaned/Web/API/NavigatorLanguage/languages -/ja/docs/Web/API/NavigatorOnLine /ja/docs/orphaned/Web/API/NavigatorOnLine -/ja/docs/Web/API/NavigatorOnLine/Online_and_offline_events /ja/docs/orphaned/Web/API/NavigatorOnLine/Online_and_offline_events -/ja/docs/Web/API/NavigatorOnLine/onLine /ja/docs/orphaned/Web/API/NavigatorOnLine/onLine +/ja/docs/Web/API/NavigatorOnLine/Online_and_offline_events /ja/docs/Web/API/Navigator/Online_and_offline_events +/ja/docs/Web/API/NavigatorOnLine/onLine /ja/docs/Web/API/Navigator/onLine /ja/docs/Web/API/NavigatorPlugins /ja/docs/orphaned/Web/API/NavigatorPlugins /ja/docs/Web/API/NavigatorPlugins/javaEnabled /ja/docs/orphaned/Web/API/NavigatorPlugins/javaEnabled /ja/docs/Web/API/NavigatorPlugins/mimeTypes /ja/docs/orphaned/Web/API/NavigatorPlugins/mimeTypes @@ -3936,7 +3935,7 @@ /ja/docs/Web/API/window.navigator.appName /ja/docs/orphaned/Web/API/NavigatorID/appName /ja/docs/Web/API/window.navigator.battery /ja/docs/Web/API/Navigator/battery /ja/docs/Web/API/window.navigator.language /ja/docs/orphaned/Web/API/NavigatorLanguage/language -/ja/docs/Web/API/window.navigator.onLine /ja/docs/orphaned/Web/API/NavigatorOnLine/onLine +/ja/docs/Web/API/window.navigator.onLine /ja/docs/Web/API/Navigator/onLine /ja/docs/Web/API/window.navigator.oscpu /ja/docs/Web/API/Navigator/oscpu /ja/docs/Web/API/window.navigator.platform /ja/docs/orphaned/Web/API/NavigatorID/platform /ja/docs/Web/API/window.navigator.plugins /ja/docs/orphaned/Web/API/NavigatorPlugins/plugins diff --git a/files/ja/_wikihistory.json b/files/ja/_wikihistory.json index 5c11c60d68..baa0d4f3bd 100644 --- a/files/ja/_wikihistory.json +++ b/files/ja/_wikihistory.json @@ -18943,6 +18943,31 @@ "Potappo" ] }, + "Web/API/Navigator/Online_and_offline_events": { + "modified": "2019-03-23T23:53:08.504Z", + "contributors": [ + "chrisdavidmills", + "ethertank", + "Marsf", + "Mgjbot", + "Koyamak" + ] + }, + "Web/API/Navigator/onLine": { + "modified": "2020-10-15T21:16:47.171Z", + "contributors": [ + "isdh", + "ryutamaki", + "yyss", + "dextra", + "fscholz", + "khalid32", + "ethertank", + "Potappo", + "Norah", + "Mgjbot" + ] + }, "Web/API/Navigator/oscpu": { "modified": "2020-10-15T21:16:44.807Z", "contributors": [ @@ -51169,37 +51194,6 @@ "nshimizu" ] }, - "orphaned/Web/API/NavigatorOnLine": { - "modified": "2019-03-23T23:00:36.403Z", - "contributors": [ - "fscholz" - ] - }, - "orphaned/Web/API/NavigatorOnLine/Online_and_offline_events": { - "modified": "2019-03-23T23:53:08.504Z", - "contributors": [ - "chrisdavidmills", - "ethertank", - "Marsf", - "Mgjbot", - "Koyamak" - ] - }, - "orphaned/Web/API/NavigatorOnLine/onLine": { - "modified": "2020-10-15T21:16:47.171Z", - "contributors": [ - "isdh", - "ryutamaki", - "yyss", - "dextra", - "fscholz", - "khalid32", - "ethertank", - "Potappo", - "Norah", - "Mgjbot" - ] - }, "orphaned/Web/API/NavigatorPlugins": { "modified": "2019-03-23T23:01:18.262Z", "contributors": [ diff --git a/files/ja/orphaned/web/api/navigatoronline/index.html b/files/ja/orphaned/web/api/navigatoronline/index.html deleted file mode 100644 index 35fed754da..0000000000 --- a/files/ja/orphaned/web/api/navigatoronline/index.html +++ /dev/null @@ -1,60 +0,0 @@ ---- -title: NavigatorOnLine -slug: orphaned/Web/API/NavigatorOnLine -tags: - - API -translation_of: Web/API/NavigatorOnLine -original_slug: Web/API/NavigatorOnLine ---- -

{{APIRef("HTML DOM")}}

- -

The NavigatorOnLine interface contains methods and properties related to the connectivity status of the browser.

- -

There is no object of type NavigatorOnLine, but other interfaces, like {{domxref("Navigator")}} or {{domxref("WorkerNavigator")}}, implement it.

- -

Properties

- -

The NavigatorOnLine interface doesn't inherit any property.

- -
-
{{domxref("NavigatorOnLine.onLine")}} {{readonlyInline}}
-
Returns a {{domxref("Boolean")}} indicating whether the browser is working online.
-
- -

Methods

- -

The NavigatorOnLine interface neither implements, nor inherit any method.

- -

Specifications

- - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#navigatoronline', 'NavigatorOnLine')}}{{Spec2('HTML WHATWG')}}No change from the latest snapshot, {{SpecName('HTML5 W3C')}}
{{SpecName('HTML5 W3C', '#navigatoronline', 'NavigatorOnLine')}}{{Spec2('HTML5 W3C')}}Snapshot of {{SpecName('HTML WHATWG')}} with its initial specification.
- -

Browser compatibility

- -

{{Compat("api.NavigatorOnLine")}}

- -

See also

- - diff --git a/files/ja/orphaned/web/api/navigatoronline/online/index.html b/files/ja/orphaned/web/api/navigatoronline/online/index.html deleted file mode 100644 index 162165986b..0000000000 --- a/files/ja/orphaned/web/api/navigatoronline/online/index.html +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: window.navigator.onLine -slug: orphaned/Web/API/NavigatorOnLine/onLine -tags: - - API - - DOM Reference - - NavigatorOnLine - - Online - - Property - - Reference -translation_of: Web/API/NavigatorOnLine/onLine -original_slug: Web/API/NavigatorOnLine/onLine ---- -
{{ApiRef("HTML DOM")}}
- -

ブラウザの接続状態を返します。このプロパティは真偽値を返して true はオンライン、false はオフラインを表します。ブラウザのネットワーク接続状態が変化するたびに、プロパティを更新します。この更新はユーザがリンクをたどる、あるいはスクリプトがリモートページを要求するときに発生します。例えばインターネットへの接続が失われた後にユーザがリンクをクリックすると、このプロパティは false を返します。

- -

このプロパティの実装は、ブラウザにより異なります。

- -

Chrome および Safari は、ブラウザがローカルエリアネットワーク (LAN) またはルータに接続できないときにオフライン、それ以外の状況では true を返します。従って、false 値が返る場合はブラウザがオフラインであると考えることができますが、true 値は必ずインターネットにアクセスできると考えることはできません。仮想イーサネットアダプタを持つ仮想化ソフトウェアを実行しているコンピュータでは常に "接続中" になるなど、偽陽性になる可能性があります。よって、実際のブラウザのオンライン状態を検出したい場合は、付加的なチェック方法を開発するべきでしょう。詳しくは HTML5 Rocks の記事 Working Off the Grid をご覧ください。

- -

Firefox および Internet Explorer は、ブラウザをオフラインモードに切り替えると false 値を送信します。Firefox 41 まで、他の状態では true 値を返していました。Firefox 41 より OS X および Windows で、実際のネットワーク接続状態に従って値を返します。

- -

window.ononline および window.onoffline イベントをリッスンすることにより、ネットワーク接続状態の変化を確認できます。

- -

構文

- -
online = window.navigator.onLine;
-
- -

- -

online には truefalse の真偽値が返されます。

- -

- -

live example をご覧ください。

- -

オンラインであるかを確認するには、以下のサンプルのように window.navigator.onLine を確認します:

- -
if (navigator.onLine) {
-  console.log('online');
-} else {
-  console.log('offline');
-}
- -

ブラウザが navigator.onLine をサポートしない場合は、上記のサンプルでは常に false/undefined が返ります。

- -

ネットワーク接続状態の変化を確認するには、以下のように window.online および window.offline をリッスンするため addEventListener を使用します:

- -
window.addEventListener("offline", function(e) { console.log("offline"); });
-
-window.addEventListener("online", function(e) { console.log("online"); });
-
- -

仕様

- - - - - - - - - - - - - - - - -
仕様書策定状況コメント
{{SpecName("HTML WHATWG", "browsers.html#navigator.online", "navigator.onLine")}}{{Spec2("HTML WHATWG")}}最初期の定義
- -

ブラウザ実装状況

- -
{{Compat("api.NavigatorOnLine.onLine")}}
- -

注記

- -

Firefox 3 で導入された新しいオフライン関連イベントと、このプロパティの詳しい説明は、Online/Offline Events‎ を参照してください。

- -

関連情報

- - diff --git a/files/ja/orphaned/web/api/navigatoronline/online_and_offline_events/index.html b/files/ja/orphaned/web/api/navigatoronline/online_and_offline_events/index.html deleted file mode 100644 index 4a136cde1c..0000000000 --- a/files/ja/orphaned/web/api/navigatoronline/online_and_offline_events/index.html +++ /dev/null @@ -1,74 +0,0 @@ ---- -title: Online and offline events -slug: orphaned/Web/API/NavigatorOnLine/Online_and_offline_events -translation_of: Web/API/NavigatorOnLine/Online_and_offline_events -original_slug: Web/API/NavigatorOnLine/Online_and_offline_events ---- -
{{outdated()}}
-

Firefox 3WHATWG Web Applications 1.0 仕様に従い Online/Offline イベントを実装します。

-

概観

-

オフラインに対応する良い Web アプリケーションを構築するためには、アプリケーションが実際にいつオフラインなのか知る必要があります。ついでに言えば、アプリケーションが 'オンライン' の状態に再び戻ったのがいつかを知る必要もあります。効果的に、その要件は次のように分解されます。

-
    -
  1. ユーザがいつオンラインに戻るか知らなければなりません。そうすることでサーバと再び同期を取ることができます。
  2. -
  3. ユーザがいつオフラインになるのかを知らなければなりません。そうすることでサーバへの要求を後で用いるようキューに貯めることができます。
  4. -
-

オンライン/オフラインイベントがこのプロセスを一般化することを手助けします。

-

あなたの Web アプリケーションは、オフラインリソースのキャッシュで確実に管理されるドキュメントを確立する必要があります。これを行うための方法を Firefox のオフラインリソースの記事で学んでください。

-

API

- -

navigator.onLinetrue/false の値を保持するプロパティで、true はオンラインを、false はオフラインを意味します。このプロパティはユーザが対応するメニューアイテム (ファイル→オフライン作業) を選択することによりオフラインモードに移行する度に更新されます。

-

加えて、このプロパティはブラウザがネットワークに接続することがそれ以降不可能になった時にもその度に更新される必要があります。次の仕様に従います。

-
- navigator.onLine 属性はユーザがリンクを押した時やスクリプトが外部のページを要求したときに、ユーザエージェントがネットワークに連絡できない場合(またはそのような試みが失敗した場合)に必ず false を返す必要があります。
-

Firefox 2 はこのプロパティをブラウザのオフラインモードが切り替わる度に更新していました。また Windows と Linux においてネットワークへの接続が失われたとき、復活したときにも更新していました。

-

このプロパティは Firefox と Internet Explorer の古いバージョンにも存在しました。(仕様はこれらの先の実装を基にしました。) 従って今すぐこのプロパティを使い始めることができます。ネットワーク状態の自動判断は Firefox 2 にて実装されました。

-

"online" および "offline" イベント

-

Firefox 3の 2 つの新しいイベントを紹介します。"online" と "offline" です。これらの 2 つのイベントは各ページの <body> にてブラウザがオンラインとオフラインのモードを切り替えるたびに発生します。加えてこのイベントは document.body から document へと浮上し、window までたどり着きます。これらのイベントはキャンセルできません (ユーザのオンラインモードやオフラインモードへの移行を防ぐことはできません)。

-

これらのイベントに対するリスナを登録することもいくつかの馴染みのある方法にて可能です。

- -

実行例

-

簡単なテストケースを実行してこれらのイベントの動作を確認することができます。 XXX When mochitests for this are created, point to those instead and update this example -nickolay

-
 <!doctype html>
- <html>
- <head>
-   <script>
-     function updateOnlineStatus(msg) {
-       var status = document.getElementById("status");
-       var condition = navigator.onLine ? "ONLINE" : "OFFLINE";
-       status.setAttribute("class", condition);
-       var state = document.getElementById("state");
-       state.innerHTML = condition;
-       var log = document.getElementById("log");
-       log.appendChild(document.createTextNode("Event: " + msg + "; status=" + condition + "\n"));
-     }
-     function loaded() {
-       updateOnlineStatus("load");
-       document.body.addEventListener("offline", function () {
-         updateOnlineStatus("offline")
-       }, false);
-       document.body.addEventListener("online", function () {
-         updateOnlineStatus("online")
-       }, false);
-     }
-   </script>
-   <style>...</style>
- </head>
- <body onload="loaded()">
-   <div id="status"><p id="state"></p></div>
-   <div id="log"></div>
- </body>
- </html>
-
-

参考

- -
- {{HTML5ArticleTOC()}}
diff --git a/files/ja/web/api/navigator/online/index.html b/files/ja/web/api/navigator/online/index.html new file mode 100644 index 0000000000..e10c02acbe --- /dev/null +++ b/files/ja/web/api/navigator/online/index.html @@ -0,0 +1,78 @@ +--- +title: Navigator.onLine +slug: Web/API/Navigator/onLine +tags: + - API + - DOM Reference + - Navigator + - Online + - Property + - Reference +translation_of: Web/API/Navigator/onLine +original_slug: Web/API/NavigatorOnLine/onLine +browser-compat: api.Navigator.onLine +--- +
{{ApiRef("HTML DOM")}}
+ +

ブラウザーのオンライン状態を返します。このプロパティは、オンラインを意味する true とオフラインを意味する false の論理値を返します。このプロパティは、ブラウザーのネットワークへの接続状態が変化するたびに更新を送信します。更新は、ユーザーがリンクをたどったときや、スクリプトがリモートページを要求したときに発生します。例えば、ユーザーがインターネット接続を失った直後にリンクをクリックした場合、このプロパティは false を返します。

+ +

このプロパティの実装は、ブラウザーにより異なります。

+ +

Chrome および Safari は、ブラウザーがローカルエリアネットワーク (LAN) またはルーターに接続できないときにオフライン、それ以外の状況では true を返します。従って、false 値が返る場合はブラウザーがオフラインであると考えることができますが、true 値は必ずインターネットにアクセスできると考えることはできません。仮想イーサネットアダプタを持つ仮想化ソフトウェアを実行しているコンピュータでは常に「接続中」になるなど、誤検出になる可能性があります。よって、本当にブラウザーのオンラインである状態を検出したい場合は、チェックするための追加の手段を開発する必要があります。詳しくは HTML5 Rocks の記事 Working Off the Grid をご覧ください。

+ +

Firefox と Internet Explorer では、ブラウザーをオフラインモードに切り替えると、 false 値が送信されます。 Firefox 41 までは、それ以外の条件では true 値が返されます。 Windows の Nightly 68 で実際の動作を確認したところ、 Chrome や Safari のように LAN 接続のみを確認しているため、誤検出が発生していました。

+ +

window.ononline および window.onoffline イベントを待ち受けすることにより、ネットワーク接続状態の変化を確認することができます。

+ +

構文

+ +
online = window.navigator.onLine;
+
+ +

+ +

online には true または false の論理値が返されます。

+ +

+ +

基本的な使い方

+ +

オンラインであるかを確認するには、以下の例のように window.navigator.onLine を確認します。

+ +
if (navigator.onLine) {
+  console.log('online');
+} else {
+  console.log('offline');
+}
+ +

ブラウザーが navigator.onLine に対応していない場合は、上記の例では常に false/undefined が返ります。

+ +

ネットワーク状態の変化の待ち受け +

+ +

ネットワーク接続状態の変化を確認するには、以下の例のように addEventListener を使用して、 window.online および window.offline を待ち受けします。

+ +
window.addEventListener('offline', function(e) { console.log('offline'); });
+
+window.addEventListener('online', function(e) { console.log('online'); });
+
+ +

仕様書

+ +{{Specifications}} + +

ブラウザーの互換性

+ +

{{Compat}}

+ +

+ +

Firefox 3 で導入された新しいオフライン関連イベントと、このプロパティの詳しい説明は、Online/Offline Events‎ を参照してください。

+ +

関連情報

+ + diff --git a/files/ja/web/api/navigator/online_and_offline_events/index.html b/files/ja/web/api/navigator/online_and_offline_events/index.html new file mode 100644 index 0000000000..c0a18179d4 --- /dev/null +++ b/files/ja/web/api/navigator/online_and_offline_events/index.html @@ -0,0 +1,127 @@ +--- +title: オンライン・オフラインイベント +slug: Web/API/Navigator/Online_and_offline_events +tags: + - AJAX + - DOM + - HTML5 + - Intermediate + - NeedsUpdate + - Offline web applications + - Web Development +translation_of: Web/API/Navigator/Online_and_offline_events +original_slug: Web/API/NavigatorOnLine/Online_and_offline_events +--- +

一部のブラウザーは、 Online/Offline イベントWHATWG Web Applications 1.0 仕様書に従って実装しています。

+ +

概要

+ +

オフラインに対応した良いウェブアプリケーションを構築するためには、アプリケーションが実際にオフラインになったタイミングを知る必要があります。また、アプリケーションが「オンライン」の状態に復帰したタイミングも知る必要があります。要件を分解すると次のようになります。

+ +
    +
  1. サーバーとの再同期ができるように、ユーザーがオンラインに戻ったタイミングを知る必要があります。
  2. +
  3. サーバーへのリクエストを後回しにすることができるように、ユーザーがオフラインなったタイミングを知る必要があります。
  4. +
+ +

オンライン/オフラインイベントがこのプロセスを一般化するのに役立ちます。

+ +

残念ながら、これらのイベントは完全には信頼できません。より高い信頼性が必要な場合や、 API がブラウザーに実装されていない場合は、サービスワーカーを使用したり XMLHttpRequest からのレスポンスを使用するなど、他の兆候を利用してオフラインであるかどうかを検出することができます。

+ +

API

+ + + +

navigator.onLine は、 true/false の値を保持しているプロパティです (オンラインの場合は true、オフラインの場合は false)。

+ +

このプロパティは、ユーザーが「オフラインモード」に切り替えたとき (Firefox の場合は、ファイル→オフライン作業) に更新されます。さらに、このプロパティは、ブラウザーがネットワークに接続できなくなったときに更新されます。仕様書によれば、次のようになっています。

+ +
+ navigator.onLine 属性はユーザーがリンクを辿ったり、スクリプトが外部のページをリクエストしたりしたときにユーザーエージェントがネットワークで通信できない場合 (またはそのような試みが失敗した場合) は、必ず false を返す必要があります。
+ +

Firefox 2 はこのプロパティをブラウザーのオフラインモードが切り替わる度に更新します。 Firefox 41 は、 Windows、 Linux、 OS X においてネットワークの接続状態が変化したときにも更新します。

+ +

このプロパティは Firefox と Internet Explorer の古いバージョンにも存在しました。 (この仕様はこれらの先行的な実装に基づいています)。従って、今すぐこのプロパティを使い始めることができます。ネットワーク状態の自動判断は Firefox 2 にて実装されました。

+ +

"online" および "offline" イベント

+ +

Firefox 3 は 2 つの新しいイベントを導入しました。"online" and "offline" です。これらの 2 つのイベントは、ブラウザーのオンラインおよびオフラインモードが切り替わるたびに、各ページの <body> に発行されます。加えて、このイベントは document.body から document へとバブリングし、 window までたどり着きます。これらのイベントはキャンセルできません (ユーザーのオンラインモードやオフラインモードへの移行を防ぐことはできません)。

+ +

Firefox 41 では、 Windows, Linux, OS X においてネットワークの接続状態が変化したことを OS が通知したときに、これらのイベントが発行されます。

+ +

これらのイベントに対するリスナーは、いくつかのお馴染みの方法で登録することができます。

+ + +

+ +

イベントが機能しているかどうかを確認するために、簡単なテストケースを用意しました (イベントリスナーを document.body に代入しているため、 Chrome では動作しません)。

+ +

こちらが JavaScript 部分です。

+ +
window.addEventListener('load', function() {
+  var status = document.getElementById("status");
+  var log = document.getElementById("log");
+
+  function updateOnlineStatus(event) {
+    var condition = navigator.onLine ? "online" : "offline";
+
+    status.className = condition;
+    status.innerHTML = condition.toUpperCase();
+
+    log.insertAdjacentHTML("beforeend", "Event: " + event.type + "; Status: " + condition);
+  }
+
+  window.addEventListener('online',  updateOnlineStatus);
+  window.addEventListener('offline', updateOnlineStatus);
+});
+ +

CSS 部分です。

+ +
#status {
+  position: fixed;
+  width: 100%;
+  font: bold 1em sans-serif;
+  color: #FFF;
+  padding: 0.5em;
+}
+
+#log {
+  padding: 2.5em 0.5em 0.5em;
+  font: 1em sans-serif;
+}
+
+.online {
+  background: green;
+}
+
+.offline {
+  background: red;
+}
+
+ +

対応する HTML です。 XXX When mochitests for this are created, point to those instead and update this example -nickolay

+ +
<div id="status"></div>
+<div id="log"></div>
+<p>This is a test</p>
+
+ +

実行例はこちらです。

+ +

{{ EmbedLiveSample('Example', '100%', '150') }}

+ +

+ +

この API がブラウザに実装されていない場合は、サービスワーカーや XMLHttpRequest からのレスポンスを使用するなど、他の兆候を使用してオフラインであるかどうかを検出することができます。

+ +

参考文献

+ + -- cgit v1.2.3-54-g00ecf