I admin a wiki site running MediaWiki 1.29 and I have a problem collapsing the TOC on pages.

I would be interesting in keeping the Contents box, but loading the page with it collapsed by default.

It appears there is a simple solution here https://www.mediawiki.org/wiki/Manual_talk:Table_of_contents#Improved_Solution, but I fail to implement it and I have no idea where the error is, hopefully someone can help.

I integrated the code as explained and checked that MediaWiki:Common.js is used by the site.

During page rendering, I checked the Java code is loaded and executed, but it appears to fail because

ReferenceError: toggleToc is not defined

I also checked this page https://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_(users)#MediaWiki_1.29 , but in the table there is a empty cell where it should be explained how to migrate toggleToc();. I am not even entirely sure it should be migrated.

Any help on this topic will be appreciated.



2 Answers 2


In the media wiki link provided above, it explains that the following code needs to be implemented:

// 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.
  } catch (exception) {
    // Probably this page doesn't have a TOC, ignore the exception to prevent console clutter.
}, false);

If you have implemented the above code on your website and it has not be functioning properly, my best understanding of the code above is as follows:


This listens for the page content to be loaded, then performs the following function

if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') {     toggleToc(); 

If the elements in name="toc" are NOT set to display:none, then perform the function "toggleToc"

You first must make sure that the element defined by name="toc" is not set to display:none. And then you must make sure that somewhere on your page or in your .js files that the function toggleToc has been set. If toggleToc has NOT yet been set or defined then you must write this code yourself or find the code online and implement it on your page or .js files.

Since you have received the notice of:

ReferenceError: toggleToc is not defined

My best guess is that you do not actually have the function toggleToc in any of your .js files or in on your webpage. I would check to make sure that it is there.

Hope I was of help to you.

  • Thanks for your time, but unfortunately this does not help. I understand how the code works: the problem is exactly that toggleToc should be implemented by MediaWiki directly («Use the toggleToc() JavaScript function provided by MediaWiki […]»), so it should already be available. Maybe I am wrong in invoking it, but, again, I don't understand how.
    – lucamauri
    Feb 21, 2018 at 8:17
  • View the source of the page. control+f or search for "toggletoc". Next, search (control+f) for ".js". Click on those files. Search for "toggletoc" in those as well. If you can't find this function in any of that then it doesn't exist.
    – Michael d
    Feb 21, 2018 at 8:51
  • We are sure toggleToc does exist because it is mentioned in the official MediaWiki documentation. The point here is to understand how to implement it.
    – lucamauri
    Feb 22, 2018 at 17:23

After consulting with MediaWiki support desk, I finally had confirmation that the fact that the function is listed in the table How to migrate with no text in the column Replaced by means this function was removed for good.
This is not clearly explained, in my opinion.

So the function cannot be used out-of-the-box any longer.
For the ones interested in this function, the source code can be found in a MediaWiki package older than version 1.29.
The function is written in the file


I hope this can help others to sort things out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.