Skip to content

Commit

Permalink
2023/11/09 時点の英語版に基づき更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mfuji09 committed Jun 17, 2024
1 parent 194a394 commit 895f43c
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions files/ja/web/api/document/lastmodified/index.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
---
title: Document.lastModified
title: "Document: lastModified プロパティ"
short-title: lastModified
slug: Web/API/Document/lastModified
l10n:
sourceCommit: 7e4769a3d501efb76e7cf92198b0589ab28f1864
---

{{APIRef("DOM")}}

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

## 構文
##

```
var string = document.lastModified;
```
文字列です。

##

Expand All @@ -26,7 +27,7 @@ alert(document.lastModified);

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

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

```js
let oLastModif = new Date(document.lastModified);
Expand All @@ -40,56 +41,46 @@ let oLastModif = new Date(document.lastModified);
let nLastModif = Date.parse(document.lastModified);
```

##
## メモ

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

```js
// 'timestamp' を 'last_modif=timestamp' で照合
// e.g. '1687964614822' は 'last_modif=1687964614822'
const pattern = /last_modif\s*=\s*([^;]*)/;

if (
Date.parse(document.lastModified) >
parseFloat(
document.cookie.replace(
/(?:(?:^|.*;)\s*last_modif\s*\=\s*([^;]*).*$)|^.*$/,
"$1",
) || "0",
)
(parseFloat(document.cookie.match(pattern)?.[1]) || 0)
) {
document.cookie =
"last_modif=" +
Date.now() +
"; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=" +
location.pathname;
alert("ページが変更されました。");
document.cookie = `last_modif=${Date.now()}; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=${
location.pathname
}`;
alert("ページが変更されました。");
}
```
…同じ例ですが、最初の訪問をスキップします。
```js
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)) {
const pattern = /last_modif\s*=\s*([^;]*)/;

const lastVisit = parseFloat(document.cookie.replace(pattern, "$1"));
const lastModif = Date.parse(document.lastModified);

if (Number.isNaN(lastVisit) || lastModif > lastVisit) {
document.cookie = `last_modif=${Date.now()}; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=${
location.pathname
}`;

if (isFinite(lastVisit)) {
alert("ページが変更されました。");
}
}
```
> **メモ:** WebKit は時刻の文字列を UTC で返します。 Gecko と Internet Explorer はローカルタイムゾーンで時刻を返します。 (参照: [Bug 4363 – document.lastModified returns date in UTC time, but should return it in local time](https://bugs.webkit.org/show_bug.cgi?id=4363))
もし***外部のページ*が変更されたかどうか**を知りたい場合は、 [`XMLHttpRequest()` API についてのこの段落](/ja/docs/Web/API/XMLHttpRequest_API/Using_XMLHttpRequest#Get_last_modified_date)をお読みください。
外部ページが変更されたかどうかを知りたい場合、{{HTTPMethod("HEAD")}} リクエストを {{domxref("fetch()")}} API によって行い、{{HTTPHeader("Last-Modified")}} レスポンスヘッダーを調べます。
## 仕様書
Expand Down

0 comments on commit 895f43c

Please sign in to comment.