Monday, March 2, 2020

Display the Last Modified Date Using JavaScript

Display the Last Modified Date Using JavaScript When youre reading content on the Web, its often useful to know when that content was last modified to get an idea of whether it may be outdated. When it comes to blogs, most include dates of publication for new content posted. The same is true for many news sites and news articles. Some pages, however, dont offer a date for when a page was last updated. A date isnt necessary for all pages- some information is evergreen. But in some cases, knowing the last time a page was updated is important. Even though a page may not include a last updated date, there is a simple command that will tell you this, and it doesnt require you to have a lot of technical knowledge. JavaScript Command to Display the Date of the Last Modification To get the date of the last update on a page youre currently, on, simply type the following command into the address bar of your browser and press Enter or select the Go button: javascript:alert(document.lastModified) A JavaScript alert window will pop open displaying the last date and time the page was modified. For users of the Chrome browser and some others, if you cut-and-paste the command into the address bar, be aware that the javascript: part is removed. This doesnt mean you cant use the command. You will just need to type that bit back into the command in the address bar. When the Command Doesnt Work Technology for web pages changes over time, and in some cases the command to find out when a page was last modified wont work. For example, it wont work  on sites where the page content is generated dynamically. These types of pages are, in effect, being modified with each visit, so this trick doesnt help in these cases. An Alternative Method: The Internet Archive Another means of finding when a page was last updated is using the Internet Archive, also known as the Wayback Machine. In the search field at the top, enter the full address of the web page you want to check, including the http:// part. This wont give you a precise date, but you may be able to  get an approximate idea of when it was last updated. Note, though, that the calendar view on the Internet Archive site only indicates when the Archive has crawled or visited and logged the page, not when the page was updated or modified. Adding a Last Modified Date to Your Web Page If you have a webpage of your own, and you would like to show visitors when your page was last updated, you can do this easily by adding some JavaScript code to your pages HTML document. The code utilizes the same call shown in the previous section: document.lastModified: This will display text on the page in this format: Last updated on  08/09/2016 12:34:12 You can customize the text preceding the date and time displayed by changing the text between the quotation marks- in the above example, that is the Last updated text (note that there is a space after on so that the date and time arent displayed abutting the text).

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.