Shortest way to print current year in a website

前端 未结 12 548
鱼传尺愫
鱼传尺愫 2020-12-07 06:45

I need to update a few hundred static HTML pages that have the copyright date hard coded in the footer. I want to replace it with some JavaScript that will automatically upd

12条回答
  •  再見小時候
    2020-12-07 07:27

    This is the best solution I can think of that will work with pure JavaScript. You will also be able to style the element as it can be targeted in CSS. Just add in place of the year and it will automatically be updated.

    //Wait for everything to load first
    window.addEventListener('load', () => {
        //Wrap code in IIFE 
        (function() {
            //If your page has an element with ID of auto-year-update the element will be populated with the current year.
            var date = new Date();
            if (document.querySelector("#auto-year-update") !== null) {
                document.querySelector("#auto-year-update").innerText = date.getFullYear();
            }
        })();
    });
    

提交回复
热议问题