Author Topic: Automatic "Last Modified" for web pages  (Read 358 times)

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2339
Automatic "Last Modified" for web pages
« on: March 09, 2013, 07:38:25 PM »
Not strictly Linux, but some of you may dabble in web sites, so here's my 2 cents worth.

I've always thought it was a good idea fo have some idea how old the stuff on a web page is.  No sense   getting all fired up over something 4 years old. 

So, having a programming background, I set out to not only accomplish it, but to automate it.  And here it is, for any that are interested.


1.  Include your JavaScript file in each html file by placing this in the head section:

Code: [Select]
<script type="text/javascript" language="JavaScript" src="scripts/main.js"></script>

Of course you can change the location and name to suit.


2.  Then, put this function in your javascript file:

Code: [Select]
function PutDateModified()
{
  // Get the actual date the html file was last modified, and pull out a
  //   sub-string without the seconds (no meed to make this overly
  //   complicated for the reader).  You can leave them in if you want.
  // And, in fact, you can, if you want, eliminate the variable and replace
  //   it, where it's used, with the stuff we're setting it equal to.
  var date = document.lastModified.substr(0, document.lastModified.lastIndexOf(":"));
 
  // Now. write into the html stream whatever styling you want.
  document.write("<font size="   3  " color="   "red"   ">");
  document.write("<i>");

  // Here we write the actual text into the html stream.
  document.write("Page Last Updated: "   date);

  // And finally, close out the tags.  This won't be needed if you're ALWAYS
  //   going to place as the last thing on the page, but I recommend it, just
  //   to avoid surprises later.
  document.write("</i>");
  document.write("</font>");
}

Of course, you're free to screw around with font size.color and text decoration to your hearts content.  And the styling could always be placed in a CSS style sheet.  Just don't get too gaudy!


3.  Now, down at the bottom (or where ever you prefer) of each html file, place this:

Code: [Select]
<script>
  PutDateModified();
</script>

Voila!  Every page of your web site will now automatically tell the user how old it is,  no need for you to concern yourself with it any longer.  And no, I won't sue you - it's free to use anywhere, anytime by anybody.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline YouCanToo

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 5383
  • Location: Lebanon, OR., USA
    • Spreading the word.......
Re: Automatic "Last Modified" for web pages
« Reply #1 on: March 09, 2013, 09:51:53 PM »
It is always a better practice/habit to close all open tags when coding.




Be sure to visit the NEW Knowledge Base


Linux is user-friendly- it's just picky who its friends are!

Offline Rudge

  • Hero Member
  • *****
  • Posts: 9773
  • I'm Just A Dog.
Re: Automatic "Last Modified" for web pages
« Reply #2 on: March 09, 2013, 10:18:19 PM »
It also appears that the date will be updated no matter what was changed? 

I have been known to update a page because I noticed that a "period"  was missing at the end of a sentence.

It looks very useful as long as the viewer understands that not all updates to the page are relevant.


Hay,, I had an extra $0.2 in my pocket.   ;D ;D 


-If you wish to make an apple pie from scratch, you must first invent the universe-  Carl Sagan

Offline gseaman

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 3881
Re: Automatic "Last Modified" for web pages
« Reply #3 on: March 09, 2013, 10:21:40 PM »
It also appears that the date will be updated no matter what was changed? 

I have been known to update a page because I noticed that a "period"  was missing at the end of a sentence.

It looks very useful as long as the viewer understands that not all updates to the page are relevant.


Hay,, I had an extra $0.2 in my pocket.   ;D ;D 

Yes, but changing that period also confirms that the rest of the page is still valid. :)
$0.02 from me, too!

Galen

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2339
Re: Automatic "Last Modified" for web pages
« Reply #4 on: March 10, 2013, 11:31:52 AM »
It also appears that the date will be updated no matter what was changed? 

Yes, the date given is the date the file was last modified.  You can insert a character, immediately delete it, and most editors will indicate the file has been modified, and let you save it.  When you do, that is the date/time that will be displayed.

Quote
It looks very useful as long as the viewer understands that not all updates to the page are relevant.

It still indicates someone has looked at the page.  As the web master on a site, it will help me out.  It will be quick and easy to surf through the site and take note of those pages that are getting a bit stale.  I can also do that on my local copy, even without an internet connection.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline seaeagle1965

  • Jr. Member
  • **
  • Posts: 42
    • Mega Free Wallpapers
Re: Automatic "Last Modified" for web pages
« Reply #5 on: March 10, 2013, 11:46:54 AM »
For those into php, this is the code I use on my pages to indicate when they were last updated:

Code: [Select]
<?php
$updfile 
__FILE__;
$updlastmod date("l, F j, Y @ g:ia"filemtime($updfile));
echo(
"<small><i>Page last modified: $updlastmod</i></small>");
?>

You can split the code if you want:

To be placed in the file to be dated:
Code: [Select]
<?php
$updfile 
__FILE__;
$updlastmod date("l, F j, Y @ g:ia"filemtime($updfile));
?>

You can place this bit on the same page or in your site's template:
Code: [Select]
<?php
echo("<small><i>Page last modified: $updlastmod</i></small>");
?>
Intel Q9400 Core 2 Quad CPU @ 2.66GHz; GeForce 210 video; 4GB RAM; 160GB SCSI HDD + 1.5TB external drive
seaeagle1965 on Twitter

Offline footstep11

  • Full Member
  • ***
  • Posts: 147
  • Linux User: #63879
Re: Automatic "Last Modified" for web pages
« Reply #6 on: March 11, 2013, 07:27:27 AM »
...or just plain html:

Quote
<!--#echo var="LAST_MODIFIED"-->

You can modify the date format with the line (this is just an example. See the syntax of the date command on a linux system)

Quote
<!--#config timefmt="%A, %d-%b-%Y %X %Z" -->

that will display something like (this is the output from my homepage)
"Tuesday, 28-Aug-2012 13:46:02 CEST"

Ciao, Mauro
Desktop with Intel Core2 Duo CPU E8500 @ 3.16GHz
nVidia GeForce 9400 GT,  4GB DDR2 RAM
‎82801I (ICH9 Family) HD Audio Controller
82566DC-2 Gigabit Network Connection
PCLinuxOS 32Bit, FullMonty (KDE)
Linux kernel 3.4.49-pclos1.pae

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2339
Re: Automatic "Last Modified" for web pages
« Reply #7 on: March 11, 2013, 09:21:25 AM »
...or just plain html:

Quote
<!--#echo var="LAST_MODIFIED"-->

You can modify the date format with the line (this is just an example. See the syntax of the date command on a linux system)

Quote
<!--#config timefmt="%A, %d-%b-%Y %X %Z" -->

I'm confused - those are comments, how can they do anything?

that will display something like (this is the output from my homepage)
"Tuesday, 28-Aug-2012 13:46:02 CEST"

Ciao, Mauro

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline kjpetrie

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 4037
Re: Automatic "Last Modified" for web pages
« Reply #8 on: March 11, 2013, 01:10:03 PM »
No, they're not comments, they're server-side includes (aka SHTML). They will be treated as comments if they ever reach the browser, but SSI will parse them as instructions and obey them.

PHP has largely superseded older technologies like SSI/CGI in most usage scenarios, but you can still set them up and use them if you like.

I've been using PHP for this for 5 years. JavaScript is useful on the client side, but you can never be sure the client will support it. What you do server side needs no support in an unknown system.
-----------
KJP
-----------------------------------------------------------
PClos64 RC1 on Intel D945GCLF2 motherboard (Atom 330), 2GB DDR2 RAM, Maxtor STM325031, HL-DT-ST DVDRAM GSA-H42N, Amilo LSL 3220T monitor. Also Acer 5810TG (with custom kernel) and Asus eeePC 2G surf

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2339
Re: Automatic "Last Modified" for web pages
« Reply #9 on: March 11, 2013, 03:56:36 PM »
No, they're not comments, they're server-side includes (aka SHTML). They will be treated as comments if they ever reach the browser, but SSI will parse them as instructions and obey them.

OK - I've never messed with it.  In fact, I just got into this whole web development stuff a couple of years ago when the clubs web master left and no one else would touch it.  I kinda got it by default.  I've spent a lot of time  with the W3C tutorials to get where I am.

Speaking of includes, being an old C/C++ programmer, the one thing I really miss in html is the #include statement.  It would make so many things so much easier to maintain.  Like a menu... 

Yeah, I know - iframe.  But then it doesn't know anything about the parents data structures and variables.  I use this for menu entries:

Style:
Code: [Select]
.menu_entry
{
  border-style: solid;
  border-width: 2px;
  border-color: black;
  border-radius: 5px;
  padding: 5px;
  font-size: 18.0px;
  text-align: center;
  color: green;
  background-color: lightgreen;
}

Script:
Code: [Select]
function PutMenuEntry(file, title)
{
  if (document.title != title)
  {
    document.write("<div class=" + "menu_entry" + ">");
    document.write("<a href=" + file + ">" + title + "</a>");
    document.write("</div>");
    document.write("<br>");
  }
   
}


And call it  with name of the file, and the text to appear in the menu, but I make sure that text is the same as the title of the target  page.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline kjpetrie

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 4037
Re: Automatic "Last Modified" for web pages
« Reply #10 on: March 11, 2013, 05:29:45 PM »
Look up Apache mod_include for the SSI <!--#include --> statement or use the PHP include statement. These will provide what you want. No sane web developer would try to maintain a site without them!

Computers exist to do the work for us. That includes web servers.
-----------
KJP
-----------------------------------------------------------
PClos64 RC1 on Intel D945GCLF2 motherboard (Atom 330), 2GB DDR2 RAM, Maxtor STM325031, HL-DT-ST DVDRAM GSA-H42N, Amilo LSL 3220T monitor. Also Acer 5810TG (with custom kernel) and Asus eeePC 2G surf