Document: lastModified-Eigenschaft

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Die lastModified-Eigenschaft des Document-Interfaces gibt einen String zurück, der das Datum und die lokale Uhrzeit enthält, zu der das aktuelle Dokument zuletzt geändert wurde.

Wert

Ein String.

Beispiele

Einfache Anwendung

Dieses Beispiel zeigt einen Hinweis mit dem Wert von lastModified an.

js
alert(document.lastModified);
// returns: Tuesday, December 16, 2017 11:09:42

Umwandlung von lastModified in ein Date-Objekt

Dieses Beispiel wandelt lastModified in ein Date-Objekt um.

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

Umwandlung von lastModified in Millisekunden

Dieses Beispiel wandelt lastModified in die Anzahl der Millisekunden seit dem 1. Januar 1970, 00:00:00, lokale Zeit, um.

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

Hinweise

Beachten Sie, dass lastModified als String nicht einfach für den Vergleich der Änderungsdaten von Dokumenten verwendet werden kann. Hier ist ein mögliches Beispiel, wie eine Hinweisnachricht angezeigt werden kann, wenn sich die Seite ändert (siehe auch: JavaScript-Cookies-API):

js
// Match 'timestamp' in 'last_modif=timestamp'
// e.g. '1687964614822' in '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("This page has changed!");
}

...das gleiche Beispiel, aber beim ersten Besuch ignoriert:

js
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("This page has been changed!");
  }
}

Wenn Sie wissen möchten, ob sich eine externe Seite geändert hat, können Sie eine HEAD-Anfrage mit der fetch()-API stellen und den Last-Modified-Antwortheader überprüfen.

Spezifikationen

Specification
HTML
# dom-document-lastmodified-dev

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
lastModified

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.