|
JavaScript Articles
XML Menu Builder
Build powerful, client-side DHTML menus using XML for data and CSS for style. No JavaScript data! No having to place list elements on every single page of your website! No plug-ins!
Dynamically Updating Copyright Dates Have you ever looked at the the bottom of a web page and the copyright date isn't updated to reflect the current year? Sometimes the copyright is a year out of date. Sometimes it is more than a year out of date. When I see a website like this, it means to me that the company has folded or is operating under a skeleton crew. Or, probably more likely, the people running the website are just incompetent. In any case, you don't want to do business with people like this. COPYRIGHT = "Copyright © My Company Inc., 1990-";function writeCopyright() { document.write(COPYRIGHT, new Date().getFullYear(), ". All rights reserved."); } When the writeCopyright() method in the source code above is executed, it produces the following output:
Note that the starting copyright date of 1990 is just an arbitrary number used for the purpose of this example. You can set this to whatever you want. Here is the code for a bare-bones HTML page which uses the writeCopyright() method to dynamically create a copyright statement in the center of the page: <html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script> COPYRIGHT = "Copyright © My Company Inc., 1990-"; function writeCopyright() { document.write(COPYRIGHT, new Date().getFullYear(), ". All rights reserved."); } </script> </head> <body> <div align="center"> <script>writeCopyright();</script> </div> </body> </html> This works for an example, but for your web site, you definitely want to move the JavaScript code in the <head> tags to a separate .js file to remove unnecessary redundancy and clutter. Caveats: Some people will jump on me because I wasn't expressing the copyright string properly. They will say that a copyright cannot technically span years. My response to this is, I'm not a lawyer, I don't know the technicalities of the copyright law, but if it's good enough for Microsoft® — it's good enough for me, and if you don't like it, you can write your own routine, using these same principals to dynamically create a copyright string that'll look something like this: Copyright © My Company Inc., 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002. All rights reserved. All I could say is, God forbid your company lasts for another few centuries, as your poor users will have pages of copyright info to contend with. The only real problem I can see is that the client's computer may have a screwed up date. So if your client has his date set to say, 1840, my writeCopyright() method will write 1840, because that is the current year according to a computer with a screwed up date. My personal feeling is that if a person has the date on their computer set to 1840, you don't have to worry about him because he is so out-to-lunch that a copyright statement isn't going to mean anything to him. He's going to go ahead and take your property and he's not even going to think twice about whether or not there is a copyright statement. He's not even going to feel guilty about it. If you're that worried about the guy who has his date set to 1840, run a CGI program to write the date. This will fix the problem but the drawbacks are that it adds more overhead on your server. Also, if you switch to a different server, the CGI program may not be available. It is basically up to you. If we felt the CodeHouse.com website had a server we could reliably call home, we'd probably opt for the CGI solution, but we don't, and the current solution is vastly better than manually updating all of our web pages at the start of each new year. You're just asking for trouble when you do stuff like that. Written by: Moshe Moskowitz |