Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM lastModified 속성 – 문서 최종 수정 일시 확인하기

HTML DOM의 lastModified 속성은 현재 문서가 마지막으로 수정된 날짜와 시간을 문자열 형태로 반환합니다. 이 속성은 읽기 전용(read-only)이며, 별도의 매개변수 없이 간단하게 호출할 수 있어 웹페이지의 최신 업데이트 정보를 사용자에게 표시할 때 유용하게 활용됩니다.

구문

lastModified 속성의 기본 구문은 다음과 같습니다.

document.lastModified

예제

다음 예제를 통해 HTML DOM lastModified 속성의 실제 사용 방법을 살펴보겠습니다. 이 예제에서는 버튼을 클릭하면 해당 페이지(공지)가 마지막으로 수정된 날짜와 시간을 화면에 출력합니다.

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM lastModified</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-lastModified</legend>
<label for="textSelect">NOTICE:
<input type="text" size="25" id="textSelect" value="Next Sunday will be a working" disabled>
</label>
<input type="button" onclick="getNoticeIssuedTime()" value="When was this issued?">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var textSelect = document.getElementById("textSelect");
   function getNoticeIssuedTime() {
      divDisplay.textContent = 'Notice was issued on: '+document.lastModified;
   }
</script>
</body>
</html>

출력 결과

위 코드를 실행하면 다음과 같은 결과를 확인할 수 있습니다.

'When was this issued?'(언제 발행되었나요?) 버튼을 클릭하기 전 화면입니다.

HTML DOM lastModified 속성 – 문서 최종 수정 일시 확인하기

버튼을 클릭한 후에는 아래와 같이 document.lastModified 값이 반환되어 공지가 마지막으로 수정된 날짜와 시간이 표시됩니다.

HTML DOM lastModified 속성 – 문서 최종 수정 일시 확인하기