MediaWiki:Common.js

From Know Islam
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

function calculateAgeAtDeath() {
    var birthElems = document.querySelectorAll('[data-birth]');
    var deathElems = document.querySelectorAll('[data-death]');
    birthElems.forEach(function(birthElem, index) {
        var birthDate = new Date(birthElem.getAttribute('data-birth'));
        var deathDate = new Date(deathElems[index].getAttribute('data-death'));
        if (!isNaN(birthDate) && !isNaN(deathDate)) {
            var age = deathDate.getFullYear() - birthDate.getFullYear();
            var m = deathDate.getMonth() - birthDate.getMonth();
            if (m < 0 || (m === 0 && deathDate.getDate() < birthDate.getDate())) {
                age--;
            }
            birthElem.parentNode.querySelector('.age-at-death').innerText = age;
        }
    });
}

document.addEventListener('DOMContentLoaded', calculateAgeAtDeath);