February 24, 2024

Javascript automatically update copyright year

Purpose

When I made some changes to my Gallery page today, I realised the copyright year does not display correctly, it is still displayed for the year 2023 instead of the year 2024.


The blog template engine is Hexo. When I navigate to the source code of this section, I understand why it was not displayed correctly.

截屏2024-02-24 下午3.25.40

截屏2024-02-24 下午3.30.04

The copyright year was displayed based on the year of the post-written year so of course it will not update automatically.



Fix

Instead of displaying the copyright year based on the post-written year, I want it could be updated automatically which is always keeping up to date.

It is an easy fix by adding a simple script within the copyright information and letting it retrieve the current year instead of the post-written year.

1
<script>document.write(new Date().getFullYear())</script>

截屏2024-02-24 下午3.26.37



Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#Javascript