Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
Created page with "Any JavaScript here will be loaded for all users on every page load.: $(document).ready(function() { $('.mw-collapsible').on('click', function() { $('.mw-collapsible').not(this).addClass('mw-collapsed'); }); });"
 
SEO: backfill meta description + image alt attributes
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Site JS — loaded on every page (MediaWiki:Common.js). */


$(document).ready(function() {
/* SEO fixes (Lighthouse "meta description" + "image alt"). Citizen renders
    $('.mw-collapsible').on('click', function() {
  og:description but not <meta name="description">, and MediaWiki renders inline
        $('.mw-collapsible').not(this).addClass('mw-collapsed');
  images with no alt attribute, so we backfill both client-side. */
    });
( function () {
});
// 1. Mirror og:description into a real meta description if missing.
if ( !document.querySelector( 'meta[name="description"]' ) ) {
var og = document.querySelector( 'meta[property="og:description"]' );
if ( og && og.content ) {
var m = document.createElement( 'meta' );
m.setAttribute( 'name', 'description' );
m.setAttribute( 'content', og.content );
document.head.appendChild( m );
}
}
 
// 2. Give every image an alt attribute if it lacks one, derived from the
//    wrapping link's title (page/script name) or the file name.
document.querySelectorAll( 'img:not([alt])' ).forEach( function ( img ) {
var link = img.closest( 'a' );
var alt = ( link && link.getAttribute( 'title' ) ) || '';
if ( !alt ) {
var src = img.getAttribute( 'src' ) || '';
alt = decodeURIComponent( src.split( '/' ).pop() || '' )
.replace( /^\d+px-/, '' )
.replace( /\.[a-z0-9]+$/i, '' )
.replace( /[_-]+/g, ' ' )
.trim();
}
img.setAttribute( 'alt', alt );
} );
}() );