This JavaScript code below is an event listener that executes when the DOM (Document Object Model) is fully loaded and ready.
Here's an explanation of how it works:
-
The code is wrapped within the DOMContentLoaded event listener, which triggers when the HTML document has been completely loaded and parsed.
-
Inside the event listener, a Date object is created using new Date(). This object represents the current date and time.
-
The getFullYear() method is called on the Date object to retrieve the current year as a four-digit number.
-
The next line selects an HTML element with the ID "current-year" using document.getElementById('current-year'). This assumes that there is an HTML element in the document with the ID "current-year" (e.g., <span id="current-year"></span>).
-
The textContent property is used to set the content of the selected element to the value of the year variable, which contains the current year.
-
When the DOM is fully loaded, the event listener triggers, and the current year is inserted into the HTML element with the ID "current-year".
To use this code, follow these steps:
-
Add an HTML element with the ID "current-year" to the appropriate location in your HTML markup. For example: <span id="current-year"></span>.
-
Place the given JavaScript code within a <script> tag either in the head section or just before the closing body tag of your HTML document.
<!DOCTYPE html><html>
<head>
<!-- Other head elements - https://websitefunctions.com -->
<script>
document.addEventListener('DOMContentLoaded', function() {
var year = new Date().getFullYear();
var yearElement = document.getElementById('current-year');
yearElement.textContent = year;
});
</script>
</head>
<body>
<!-- Rest of your HTML content -->
<footer class="footer"><div class="row"><div class="col-sm-8"><p class="footerleft"> <a href="/terms-of-use">Terms of use</a> | <a href="/privacy-policy">Privacy policy</a> | <a href="/contact">Contact</a></p></div><div class="col-sm-4"><p>Wordpress Functions - <span id="current-year"></span></p></div></div></footer></body>
</html>
When the page is loaded, the JavaScript code will be executed, and the element with the ID "current-year" will be updated with the current year.