A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.cdn.mozilla.net/ja/docs/Web/API/Document/lastModified below:

Document: lastModified プãƒãƒ‘ティ - Web API

Document: lastModified プロパティ

Baseline Widely available

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

値

文字列です。

例 単純な使用

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

alert(document.lastModified);
// 表示内容: Tuesday, December 16, 2017 11:09:42
lastModified を Date オブジェクトへ変換

この例では、 lastModified を Date オブジェクトに変換します。

let oLastModif = new Date(document.lastModified);
lastModified をミリ秒に変換

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

let nLastModif = Date.parse(document.lastModified);
メモ

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

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

if (
  Date.parse(document.lastModified) >
  (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("ページが変更されました。");
}

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

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("ページが変更されました。");
  }
}

外部ページが変更されたかどうかを知りたい場合、HEAD リクエストを fetch() API によって行い、Last-Modified レスポンスヘッダーを調べます。

仕様書 ブラウザーの互換性

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4