--- title: Document.lastModified slug: Web/API/Document/lastModified tags: - API - Document - HTML DOM - NeedsSpecTable - Property - Reference translation_of: Web/API/Document/lastModified ---
lastModified は {{domxref("Document")}} インターフェイスのプロパティで、現在の文書が最後に更新された日付と時刻を含む文字列を返します。
var string = document.lastModified;
この例では lastModified の値をアラート表示します。
alert(document.lastModified); // 表示内容: Tuesday, December 16, 2017 11:09:42
この例では、 lastModified を {{jsxref("Date")}} オブジェクトに変換します。object.
let oLastModif = new Date(document.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("ページが変更されました。");
}
}
もし外部のページが変更されたかどうかを知りたい場合は、 XMLHttpRequest() API についてのこの段落をお読みください。
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('HTML WHATWG', '#dom-document-lastmodified', 'document.lastModified')}} | {{Spec2('HTML WHATWG')}} |