MediaWiki:Gadget-sectionAnchors.js

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.

  • In most Windows and Linux browsers: Hold down Ctrl and press F5.
  • In Safari: Hold down ⇧ Shift and click the Reload button.
  • In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
/**
 * Vector HeadAnchors
 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-vector-headanchor.js
 * @revision 2015-03-07
 *
 * @license http://krinkle.mit-license.org/
 * @author Timo Tijhof, 2013–2015
 * 
 * See also:
 * https://www.mediawiki.org/wiki/Requests_for_comment/Clickable_section_anchors
 * https://phabricator.wikimedia.org/T18691
 */
$( function () {
    
    if ( mw.config.get('wgIsMainPage') ) {
        // Don't run on the main page, where we use section headings extensively
        return
    }

    $( '.mw-headline' ).each( function ( i, el ) {
        var id = el.id,
            $headline = $( el ),
            $header = $headline.parent();

        // If MediaWiki core's anchor is here, or if ours is here already, skip.
        if ( !id || $header.find( '.mw-headline-anchor,.mw-headline-headanchor').length ) {
            return;
        }

        $headline.removeAttr('id')
        $header.addClass( 'mw-header' ).prepend(
            $( '<a>' ).prop({
                id: id,
                href: '#' + id,
                title: 'Link to this section',
                className: 'mw-headline-headanchor'
            })
        );
    } );

})