Current Copyright Year For Static XHTML Pages

Print Friendly, PDF & Email

I. With Hard-Coding

Originally, the current copyright year for the entry page on this site was hard-coded in XHTML. This worked fine, except it was always necessary to remember to change the current year every January 1st:

<div id="copyright">© Chou Seh-fu 2010-2013. All rights reserved.<div>

The output was as follows:

© Chou Seh-fu 2010-2013. All rights reserved.


II. With JavaScript

In 2011, the current year was not changed on time, so JavaScript was introduced to change the current year automatically:

<div id="copyright">© Chou Seh-fu 2010-<script src="javascript/currentYear.js" type="text/javascript" ></script>. All rights reserved.<div>

The currentYear.js file being called from the site’s javascript directory is composed of only two lines:

var theDate = new Date();
document.write(theDate.getFullYear());

This was successful in outputting the current year…for users who have JavaScript enabled.

But roughly 2% of users have JavaScript disabled, and they would receive the copyright without the current year:

© Chou Seh-fu 2010-. All rights reserved.

To allow those without JavaScript to view the current year, the current year was hard-coded on the XHTML page between the <noscript> tags following the <script> tag:

<div id="copyright">© Chou Seh-fu 2010-<script src="javascript/currentYear.js" type="text/javascript" ></script><noscript>13</noscript>. All rights reserved.<div>

III. With JavaScript & Server-Side Includes

Thus, the output was automated for 98% of users. But it still required hard-coding the current year for 2% of users without JavaScript.

This could have been fully-automated by using PHP, but this was deemed overkill.

Instead, Server-Side Includes (SSI) can be used in the <noscript> tags. This eliminates the need to hard-code the current year:

<div id="copyright">© Chou Seh-fu 2010-<script src="javascript/currentYear.js" type="text/javascript" ></script><noscript> <!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL" --></noscript>. All rights reserved.<div>

This site is hosted on an Apache server, and GoDaddy seems to have set it up to automatically parse the SSI in XHTML. Therefore, it does not seem necessary to encode this in SHTML.

(Much more on Apache SSI can be found here. And here.)


IV. With Server-Side Includes Only?

Final note: Although the XHTML code in Section III is currently in use, the JavaScript can actually be dispensed with entirely. This would result in simpler code:

<div id="copyright">© Chou Seh-fu 2010-<!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL" -->. All rights reserved.<div>

Though simpler, the XHTML page generated by this code may suffer from caching effects at the beginning of a new year (ie: at the beginning of a new year, the old page containing the previous year may still be in the cache).

For this reason, the more complicated code in Section III may be preferred.


POSTSCRIPT: After some testing, it appears that caching is not a problem for the SSI code.

In that case, it is probably unnecessary for webmasters to use both JavaScript and SSI for generating copyright notices (SSI is probably preferable, given that it is viewable by everyone).


The code in this post is word-wrapped. If copying & pasting, do NOT add returns to this code!

Server-Side Includes do not work in offline testing – the XHTML file which contains them needs to uploaded to a server!

This entry was posted in Copyright Year Code, HTML & CSS, JavaScript, Server-Side Include code. Bookmark the permalink.