Pages

Friday, January 3, 2014

Keep Your Copyright Message Fresh

There’s nothing that reeks of staleness and lack of attention, than a footer saying “copyright © 2009” in 2014. Doubly so when it’s a technology site. At best it says no one cares, at worst it suggests no one updated the site, or it’s look and feel, in years. Many times it’s not even the current maintainer’s fault: the stale message resides in a footer file, that gets included automatically with every page by a CMS system.

For my sites and web applications, as well as the ones I develop for my clients, I want to keep the copyright message fresh. It at least keeps up a pretense of someone updating the site once a year Smile. And there’s no easier way to do it then with JavaScript.

All you need to do is locate the line in your footer that looks like:
<footer>copyright © 2009</footer>
and replace the year with a <span>:
<footer>copyright © <span id="year"></span></footer> 
And add this script tag to the end of the page:
<script>
    document.getElementById('year').textContent = (new Date()).getFullYear();
</script>
Even easier: if your site is using jQuery, add this to the ready event:
$('#year').text((new Date()).getFullYear());
This way, your copyright message will always show the current year. Keeping your content fresh? Well, that’s up to you Smile.

Update:
Here's a shorter way to achieve the same effect, with one line:
<footer>copyright © <script>document.write((new Date()).getFullYear());</script></footer>

[This way is also more efficient, since no DOM selection is taking place].