JavaScript Alert document.lastModified
-
Alert
document.lastModified
in JavaScript -
Convert the Last Modified Date Into a
Date
Object - Conclusion
JavaScript is a leading programming language for developing web applications. This article discusses the JavaScript property known as lastModified
.
It gives us the date and time of the web page that it recently modified. This data is obtained from the HTTP header, which sends by the web server.
Web servers get a last-modified date and time analyzing themselves.
Web servers’ documents are optional and include the last modification dates. JavaScript assumes 0 when a web server doesn’t supply a last-modified date, which corresponds to midnight on January 1, 1970, GMT.
Alert document.lastModified
in JavaScript
lastModified
is a read-only property, and it is one of the Document
interface properties, and in the web browser, it represents any web page. Also, lastModified
returns a string value.
We can check the last modified date by using the following syntax.
alert(document.lastModified)
Let’s go through the example of using lastModified
as follows.
<!DOCTYPE html>
<html>
<header>
<meta charset="UTF-8">
<meta name="viewport" content="width=divce-width, initial-scale=1">
</header>
<body>
<script>
alert(document.lastModified);
</script>
</body>
This instance popup a window on the browser displaying a string of that last date and time modified on the web page.
Output:
Also, we can set up the JavaScript code to print the last-modified date and time when the web page gets modified. This can include the web page so that users can get information about the last-modified date of the website.
<!DOCTYPE html>
<html>
<header>
<meta charset="UTF-8">
<meta name="viewport" content="width=divce-width, initial-scale=1">
</header>
<style>
*{
background-color: rgb(150, 99, 23);
}
</style>
<body>
<h2>Display last modified date and time</h2>
<p id="demo"></p>
<script>
let text = document.lastModified;
document.getElementById("demo").innerHTML = text;
</script>
</body>
We can declare document.lastModified
to variable text
and for the last modified date and time to be pictured on the web page. It can assign to the document.getElementById("demo").innerHTML
.
Following is the output we can get for the above code.
Convert the Last Modified Date Into a Date
Object
We also can convert the last modified date to the Date
object. The Date
object returns the year, day, month, and time with the time zone of a particular website.
When lastModified
is converted into a Date
object, it displays the last modified date as the output of the Date
object. Let’s look at the following instance.
<!DOCTYPE html>
<html>
<header>
<meta charset="UTF-8">
<meta name="viewport" content="width=divce-width, initial-scale=1">
</header>
<style>
*{
background-color: rgb(244, 240, 11);
}
</style>
<body>
<h2>Convert lastModified into Date object</h2>
<p id="dateObject"></p>
<script>
const last_modified= new Date(document.lastModified);
document.getElementById("dateObject").innerHTML = last_modified;
</script>
</body>
Like the example mentioned, lastModified
is declared as a variable and assigned to a new object called Date
. It does convert the lastModified
date into a Date
object.
After that, we can display a value by invoking the getElementById
function.
This code displays the last modified year, date, time, and time zone.
As in the above output, the last modified date can be seen.
Conclusion
JavaScript lastModified
property displays the last modified date and time of the particular web page. JavaScript document.lastModified
is an HTML DOM object.
This means when an HTML document loads into the browser, it becomes a document
object.
The document
object can access with a document
keyword. Therefore we can access lastModified
with the document
keyword.
Since document.lastModified
is a DOM level 3 feature (2003), it is compatible with all browsers.
Therefore, as we explore in this article, we may set an alert for a web page to display the latest changed date.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.