Requirements

Effortlessly Keep Your Copyright Up-to-Date: Automated Yearly Updates

Introduction

Changing the copyright date can be an annoying task and is easy to forget every year. Of course, you don't want the visitors to think that your website is outdated. That's why I've made a short bit of code to place on the website with which you can place "[currentYear]" anywhere, and it will change to the year that it currently is.

Snippet

var currentYear = new Date().getFullYear();
var containers = document.getElementsByTagName('footer'); // Change 'footer' to '*' if you want to change it on the whole page.
containers = Array.prototype.slice.call(containers);
var searchText = '[currentYear]';

for (var i = 0; i < containers.length; i++) {
	var elements = containers[i].getElementsByTagName('*');
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].innerText.search(searchText) > -1 && 
			elements[i].children.length === 0 &&
			elements[i].innerText.includes(searchText)) {
			elements[i].innerText = elements[i].innerText.replace(searchText,  currentYear);
			break; // Remove this line to change more than once.
		}
	}	
}

Explanation and available changes

In the snippet, you can see that we only check in the footer in this line:

var containers = document.getElementsByTagName('footer');

If you want the code to change the "[currentYear]" on the whole page, you can change 'footer' to '*'. We only check for the footer because that makes it quicker, and most of the time that's where the copyright is at. For this to work, the container in which you want the current year to be needs to have the tag-name "footer".

We also use "break" at the end of the for loop, again to make it quicker. This basically stops the code when one "[currentYear]" is changed. If you have multiple occurrences you want to change, you can just remove this.

Restricted Content

This content is available for members only. Sign up for free and explore the world of digital marketing with Grow With Groove.

Agency

Let us help you grow

Book Free 30-Minute Strategy Session