From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/document/lastmodified/index.html | 96 +++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 files/ja/web/api/document/lastmodified/index.html (limited to 'files/ja/web/api/document/lastmodified') diff --git a/files/ja/web/api/document/lastmodified/index.html b/files/ja/web/api/document/lastmodified/index.html new file mode 100644 index 0000000000..2be565e696 --- /dev/null +++ b/files/ja/web/api/document/lastmodified/index.html @@ -0,0 +1,96 @@ +--- +title: Document.lastModified +slug: Web/API/Document/lastModified +tags: + - API + - Document + - HTML DOM + - NeedsSpecTable + - Property + - Reference +translation_of: Web/API/Document/lastModified +--- +
{{APIRef("DOM")}}
+ +

lastModified は {{domxref("Document")}} インターフェイスのプロパティで、現在の文書が最後に更新された日付と時刻を含む文字列を返します。

+ +

構文

+ +
var string = document.lastModified;
+
+ +

+ +

単純な使用

+ +

この例では lastModified の値をアラート表示します。

+ +
alert(document.lastModified);
+// 表示内容: Tuesday, December 16, 2017 11:09:42
+
+ +

lastModified を Date オブジェクトへ変換

+ +

この例では、 lastModified を {{jsxref("Date")}} オブジェクトに変換します。object.

+ +
let oLastModif = new Date(document.lastModified);
+
+ +

lastModified をミリ秒に変換

+ +

この例では、 lastModified を地方時の1970年1月1日 00:00:00からの経過ミリ秒数の数値に変換します。

+ +
let nLastModif = Date.parse(document.lastModified);
+
+ +

+ +

lastModified は文字列なので、文書の更新日の比較には簡単には使用できないことに注意してください。こちらはいつページが変更されたかをアラートメッセージで表示する方法の例です (JavaScript cookies API も参照)。

+ +
if (Date.parse(document.lastModified) > parseFloat(document.cookie.replace(/(?:(?:^|.*;)\s*last_modif\s*\=\s*([^;]*).*$)|^.*$/, "$1") || "0")) {
+  document.cookie = "last_modif=" + Date.now() + "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=" + location.pathname;
+  alert("ページが変更されました。");
+}
+ +

…同じ例ですが、最初の訪問をスキップします。

+ +
var
+  nLastVisit = parseFloat(document.cookie.replace(/(?:(?:^|.*;)\s*last_modif\s*\=\s*([^;]*).*$)|^.*$/, "$1")),
+  nLastModif = Date.parse(document.lastModified);
+
+if (isNaN(nLastVisit) || nLastModif > nLastVisit) {
+  document.cookie = "last_modif=" + Date.now() + "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=" + location.pathname;
+
+  if (isFinite(nLastVisit)) {
+    alert("ページが変更されました。");
+  }
+}
+ +
注: WebKit は時刻の文字列を UTC で返します。 Gecko と Internet Explorer はローカルタイムゾーンで時刻を返します。 (参照: Bug 4363 – document.lastModified returns date in UTC time, but should return it in local time)
+ +

もし外部のページが変更されたかどうかを知りたい場合は、 XMLHttpRequest() API についてのこの段落をお読みください。

+ +

仕様書

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-document-lastmodified', 'document.lastModified')}}{{Spec2('HTML WHATWG')}}
+ +

ブラウザーの互換性

+ + + +
{{Compat("api.Document.lastModified")}}
-- cgit v1.2.3-54-g00ecf