MediaWiki:Common.js: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 11: Line 11:
             link.href = imgSrc;
             link.href = imgSrc;
             document.head.appendChild(link);
             document.head.appendChild(link);
        }
    });
    $('#mw-content-text img').each(function () {
        if (!$(this).attr('loading')) {
            $(this).attr('loading', 'lazy');
         }
         }
     });
     });
});
});

Latest revision as of 20:14, 26 June 2025

$(document).ready(function () {
    // Select all images in the content area
    $('#mw-content-text img').each(function () {
        var imgSrc = this.getAttribute('src');

        // Only proceed if the src is valid and doesn't already have a preload
        if (imgSrc && !document.head.querySelector('link[rel="preload"][href="' + imgSrc + '"]')) {
            var link = document.createElement('link');
            link.rel = 'preload';
            link.as = 'image';
            link.href = imgSrc;
            document.head.appendChild(link);
        }
    });
    $('#mw-content-text img').each(function () {
        if (!$(this).attr('loading')) {
            $(this).attr('loading', 'lazy');
        }
    });
});