As promised, here is a JavaScript code snippet which adds an automatically updating copyright date to your pages. For the PHP version of this code, see the code snippet in our article from yesterday.
The good thing about this code snippet is that it will work in any web page in which it is placed, whereas the PHP code only works in PHP pages.
To make it work, simply add the following code to your page.
<script type="text/javascript">
now=new Date();
year=now.getFullYear();
</script>© Copyright 2007-<script type="text/javascript">
document.write(year);
</script>
Be sure to update the start date so that it accurately reflects your copyright.
What will it look like? Well, in 2009, the code would output something that looks like this:
© Copyright 2007-2008
Trackback URL for this entry:
http://www.velvetblues.com/web-development-blog/javascript-code-snippet-automatically-updating-copyright-date/trackback/
Contact us today for a quote. Click here to submit details regarding your project.
If you are making a general inquiry, send an email to info@velvetblues.com
supposing that the visitor has js enabled. i think that the php snippet you gave the other day was better and sure
Indeed, the PHP version is way better. However, for someone who has never dealt with PHP and doesn’t know if their website runs on PHP, or how to save a file as PHP, this snippet will work… of course, assuming that their visitors have not disabled JavaScript.
Ironically, I did link to a related article which warns against using JavaScript.
In fact, I would do this on the server side as well and hence PHP is preferred. Also, if you really think copyright is something that changes once in a year or two. So why not just edit it manually
Haha. Well, Ajith, you are right. I used to change the copyright manually, and it really isn’t hard to do. But it is more efficient to have an automatic solution. And if you’re someone like me who manages multiple websites, it does actually save A LOT of time.
Similarly, if a website doesn’t use a content management system, then when you change the copyright, you need to go in and change each and every page. So, in that case, it is definitely worth it.
And yes, I always use the PHP solution. I prefer it too.