briank

this_year.js.html

Jan 12th, 2020 (edited)
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.49 KB | None | 0 0
  1. <script type='text/javascript'>
  2. // Update {<opt first year SEP>this year} in the document, e.g. for copyright
  3. // First year and separator are skipped in the first year
  4. // Examples: {this year}, {2025 - this year} (equivalent in 2025)
  5. // Placed in the public domain by author Brian Katzung <[email protected]>
  6. // Based on an earlier version that had a jQuery dependency
  7. // **In a Kartra membership**, remove the // on the next line to hide the code block in preview and live
  8. // if (document.querySelector('head meta[name="description"]')) document.currentScript.parentElement.parentElement.style.display = 'none';
  9. ((fillYear) => {
  10.     if (document.readyState === 'loading') {
  11.         document.addEventListener('DOMContentLoaded', fillYear);
  12.     } else {
  13.         fillYear();
  14.     }
  15. })(() => {
  16.     // Example queries:
  17.     // p (just within <p> paragraph tags)
  18.     // p,div:not(:has(div)) (paragraphs and inner-most <div> tags)
  19.     const query = 'p';
  20.     const thisYear = new Date().getFullYear();
  21.     document.querySelectorAll(query).forEach((node) => {
  22.         if (node.innerHTML.match(/{[^}]*this year}/i)) {
  23.             node.innerHTML = node.innerHTML.replace(/{[^}]*this year}/ig, (outer) => {
  24.                 const match = outer.match(/{(\d*)(.*)this year}/i);
  25.                 if (match[1] && parseInt(match[1]) !== thisYear) {
  26.                    return '' + match[1] + match[2] + thisYear;
  27.                 }
  28.                 return thisYear;
  29.             });
  30.         }
  31.     });
  32. });
  33. </script>
Advertisement
Add Comment
Please, Sign In to add comment