I have recently been running into alot of my clients comin开发者_如何学Pythong to me in the last month asking me to update their Copyright on their sites to reflect 2010.
Is it possible to write a small snippet with jQuery that automatically populated the year? When Jan 1st 2011 rolls around, the text would automatically change from 2010 to 2011?
Thanks.
$('#spanYear').html(new Date().getFullYear());
The biggest issue being that the client can change the copyright by changing the clock on their own computer ;)
You could do it via js but as David suggests its easy to manipulate from the client side. it would be better to stamp it on the server side either directly on your server via php or some other dynamic language (this would require change file extensions or config though) or by using an ajax call to a dynamic file on your server or a time service that wil return the year or full date which you can parse the year out of.
Actually you could use SSI directives for something this simple... REally old tech but the downsides of parsing .html
as .shtml
have waaay fewer reprecussions than changing all your file extenstions or parsing .html
as php or some other language. Assuming SSI is available on the server a simple:
<!--#config timefmt="%Y" -->
<!--#echo var="DATE_LOCAL" -->
should do the trick after you make the necessary html -> shtml configuration changes (which i beleive can be done in .htaccess via AddHandler
or a similar directive).
This is what I did with jQuery and I tried to use only one line:
© <span id="copyright"> <script>$('#copyright').text(new Date().getFullYear());</script> </span>
another possiblity is to make an ajax-call with jQuery to a "time-service" that will return the time/date/year and use that value to update the webpage.
edit: Yahoo has a timeservice
<script> document.write((new Date()).getFullYear()) </script>
source: http://the-chronicon.blogspot.com/2015/01/auto-update-of-copyright-year-javascript.html
精彩评论