Manual talk:Table of contents

Make TOC list Accordion sections?
Question - I have been trying to find a way to make the TOC on one page display accordion section titles, to be used on a FAQ page with only collapsed Accordions. The closest i have found of information is to implement new hooks and edit php files directly, but i would prefer to avoid this. Is there a way to accomplish this? And if not, what would be a "recommended" way to edit the files?

CSS To Change Numbering for the ToC
Stashing here for later reference. CSS i was playing with to change the numbering scheme on the ToC:

html #toc ul, .toc ul { list-style-image:none; list-style-position:inside; list-style-type:armenian /*replace this with the type of number you want*/; }

span.tocnumber { display:none; } Bawolff (talk) 03:11, 16 April 2012 (UTC)

Auto-collapse TOC on a given page?
Question - is there a way to auto-collapse the TOC on a given page? NOTOC gets rid of it, but I just want it collapsed, with the reader able to show it again should they wish. Manning Bartlett (talk) 04:08, 13 March 2013 (UTC)


 * Yes! Under the "Collapse (Hide) TOCs When Pages Load" section below, see the "Improved Solution" section. It collapses TOCs on all pages in the wiki, though, not for a specific given page. --Lance E Sloan 21:13, 31 August 2017 (UTC)

NOTOC for selected headers
Here my question, I want to have the Table of Contents in the page, but there are one header & subheaders that I don't want to show in it. Is that possible?

I show and example:

= Show 1 = == Show 1.1 == === Show 1.1.1 === == Show 1.2 == = Hide 2 = == Hide 2.1 == === Hide 2.1.1 === = Show 3 =

I hope that be clear that I mean.


 * There is no easy way: http://en.wikibooks.org/wiki/MediaWiki_User_Guide/Sections_and_Headings#Headings_not_in_TOC --Albert25 (talk) 12:32, 13 June 2013 (UTC)

Permanently repositioning the ToC directly after the title?
I cannot figure out how to permanently move the position of the Table of Contents. I have changed the CSS to float the ToC left, but I need it to come directly after the H1 heading on the page, rather than before the first H2 heading, for the text to wrap how I want it to. I can accomplish this with, but doing this on every single page is infeasible. I have not found an extension that does this either.

My question is: how would I even begin to change that? Would a skin do it? I've looked through my skin, I've looked through all the skinning manuals I can find, I even looked through SkinTemplate.php and Skin.php in the MediaWiki files, but I can't figure out which file it is -- if there even is a single file -- that simply lays out the order the different elements appear in the page code. There are a ton of variables and hooks and arrays that I suspect this is buried in, and I only have moderate programming ability so I can't dig it up on my own. It would generally be helpful to know where I'd go if I wanted to add something like a containing div around things like the ToC. Natrashafierce (talk) 20:23, 23 August 2014 (UTC)


 * I'm trying to so something similar. Cant find the answer for the life of me.
 * Vapblack (talk) 13:53, 12 December 2014 (UTC)

Solution
This is hard-code solution, if you find extension solution, please mention it. This will force to place TOC at the beginning of all documents
 * 1) Go to includes/parser/Parser.php
 * 2) Find doDoubleUnderscore method
 * 3) Add the following line at the beginning of the method
 * $text = "" . $text;


 * Hi, this method is deprecated in MW 1.34, I did not manage to find another hack with the new method. --Varlin (talk) 20:26, 7 February 2020 (UTC)

Is there a way to Apply Multilingualism to Contents Label in the TOC Template or Even Custom Title?
For example, is there a way to have the header within the TOC modified to match the applicable language if an individual selects the other language page under the navigation menu (left side) and it automatically modifies the applicable language setting, as well (top right) with that click rather than an individual having to change the language setting in top right as an additional steps? As example text, the header text would be Contents in English and Sommaire in French. Thanks!

Quadra23 (talk) 17:02, 20 January 2015 (UTC)

Header numbers but no titles
The strangest thing... I have an instance running that produces a TOC of just nested numbers, without the associated titles. The syntax of the sections hasn't changes (since when it used to work). If I poke into the html, I can see anchor tags that are blank or have "_#". So, I suspect something in MediaWiki text parser if failing to properly turn my section headers into anchors that TOC depends on.

Any hints as to how to track this bug down?

Collapse (Hide) TOCs When Pages Load
An quick an dirty way to have the TOC start collapsed

Open MediaWiki:Common.js

Add this to the js

//collaps default the TOC on opening an pages function hideToc { var toc = document.getElementById('toc').getElementsByTagName('ul')[0]; var toggleLink = document.getElementById('togglelink'); toc.style.display = 'none'; } hideToc;


 * thank you so much for this! Flylikeaseagull (talk) 07:24, 23 September 2020 (UTC)

Improved Solution
This is working for me on 1.34.0:

var toc, toggleLink; try { toc = document.getElementById('toc').getElementsByTagName('ul')[0]; toggleLink = document.getElementById('toctogglecheckbox'); // if (tocIsHidden) { toggleToc; // } } catch (error) { console.log('erred', error); } function tocIsHidden { return !toc || !toggleLink || window.getComputedStyle(toc).display !== 'block'; }

function toggleToc { var hidden = tocIsHidden; if (hidden && document.cookie.indexOf('hidetoc=0') > -1) { toggleLink.click; // changeText(toggleLink, tocShowText); // toc.style.display = 'none'; } else if (!hidden && document.cookie.indexOf('hidetoc=1') > -1) { toggleLink.click; // changeText(toggleLink, tocHideText); // toc.style.display = 'block'; } } toggleLink && toggleLink.addEventListener('click', function {  var isHidden = tocIsHidden;  document.cookie = isHidden    ? "hidetoc=1"    : "hidetoc=0"; });


 * I have some problems with the "quick an dirty" [sic] technique shown above:
 * It doesn't always work. When the code executes, the page may not have been fully loaded or parsed.  Therefore, the element with the   ID may not be available and the code will fail.  (When code is added to make it wait for the page to load and be parsed, then it works to some degree.)
 * When the code hides the TOC, it doesn't change the "hide" link text to "show". That looks bad.
 * The  variable is never used. Getting that element wastes time.
 * A better solution would:
 * Wait until until after the page has been loaded and parsed by the web browser before trying to hide the TOC.
 * Use the  JavaScript function provided by MediaWiki to:
 * Hide or show the TOC as necessary.
 * Change the "hide" link text to "show" or vice versa.
 * Set a  cookie in the web browser to indicate whether the TOC should be hidden or shown the next time a page is loaded from this wiki.
 * Add the following JavaScript code to your wiki's  page:
 * Change the "hide" link text to "show" or vice versa.
 * Set a  cookie in the web browser to indicate whether the TOC should be hidden or shown the next time a page is loaded from this wiki.
 * Add the following JavaScript code to your wiki's  page:
 * Add the following JavaScript code to your wiki's  page:

// The enclosed code runs only after the page has been loaded and parsed. window.addEventListener('DOMContentLoaded', function { try {    // Detect whether the page's TOC is being displayed.    if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') {      // Use MW's toggleToc to hide TOC, change "hide/show" link text, and set cookie.      toggleToc;    }  } catch (exception) {    // Probably this page doesn't have a TOC, ignore the exception to prevent console clutter.  } }, false);


 * Or use this shorter version without comments:

window.addEventListener('DOMContentLoaded', function { try { if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') { toggleToc; } } catch (exception) {} }, false);


 * Remember: By default, MediaWiki will not use the JavaScript in your wiki's  page.  For documentation about enabling this feature, see the following manual pages:
 * Manual:Interface/JavaScript
 * Manual:$wgUseSiteJs
 * --Lance E Sloan 21:07, 31 August 2017 (UTC)


 * I am trying to implement this solution on a MediWiki site I admin. I checked that   is loaded and that the above-proposed code is executed, but I keep getting a   error. Any idea why this is happening and how to solve it? Thanks in advance for any help you might provide Lucamauri (talk) 12:53, 18 February 2018 (UTC)

Relevent CSS rules for TOC depth.
Manual:Table of contents refers to MediaWiki:Common.css in order to find the relevant  styles, but it appears as though Common.css is just empty. I'm not sure if this is a result of most of the styles being moved into a default location and that file just being used for overrides or something. I'm not sure where to go about finding these.

Can I add Table of Content of one page to another page?
I want to make an index page that always show table of contents of another pages. I want it to always sync and don't need to update it manually. Is it possible? — Preceding unsigned comment added by 14.207.179.110 (talk • contribs) 05:39, 23 May 2017‎

Still show a little TOC button when is used
IDEA: when is used, a little [TOC} etc. or whatever tiny marker should still show up, that the user can push, revealing the. Jidanni (talk) 12:28, 12 December 2017 (UTC)

Automatically display TOC when there are 3+ headings ?
Hi, I'm wondering if there is an option somewhere (it seems not) or an easy hack to automatically display the TOC when there are 3 or more headings, instead of the treshold of 4.--Varlin (talk) 21:25, 24 January 2020 (UTC)


 * Hard-code solution :


 * 1) Go to includes/parser/Parser.php
 * 2) Replace 4 with 3 at line 4509
 * --Varlin (talk) 19:44, 7 February 2020 (UTC)

Another possible solution to hide (collapse) the Table of content by default when you open a page
.toc h2 { display: none; } .toctogglespan { display: none; } .toctogglelabel { display: none; }
 * This solution is working for me on 1.34.0 (I was unable to make it work in Vector skin with the Improved Solution above, most probably because I'm not an expert).
 * This solution is based on: Manual:Collapsible elements and CSS.
 * First, I opened Mediawiki:Vector.css and added the code below (to open Mediawiki:Vector.css, just type Mediawiki:Vector.css in the search bar and edit it):
 * 1) toc { border: 0px; }
 * Then, on a page, I added the following code, where I want my Table of content:
 * As is, it works, but it could be a better thing to create a template with the code. Example: create Template:My_TOC and add the code in it... Then on the page, just add:
 * I can't say if it can have possible side effects on something else, but for my needs...it works. If someone have some concern on this solution, it would be nice to advise... Thanks !

Is there a scroll-able support / hack for TOC in mediawiki?
When a TOC becomes too long, is it possible to make it scroll-able within a particular box? I'm not sure this is possible at the moment from my finding but I do see a hack to use the 'Scroll box' template but it does come with side effects. The effect here is that I'll have 2 boxes (the TOC box in the scroll box) which makes it not look good. I just want to simple have a box with the TOC and which is scroll-able. Thanks for your support in advance. --X-Savitar (talk) 16:08, 8 April 2020 (UTC)

The page says: is available?
What is this? I can't find it. Isn't this something en.wp specific? I found the template there. --HirnSpuk (talk) 12:01, 25 April 2021 (UTC)