Mobile app version of vmapp.org
Login or Join
Megan663

: How to use toggleToc() in a MediaWiki installation 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

@Megan663

Posted in: #Javascript #Mediawiki

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 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 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.

Thanks

Luca

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Megan663

2 Comments

Sorted by latest first Latest Oldest Best

 

@Lee4591628

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

resourcessrcmediawikimediawiki.toc.js


I hope this can help others to sort things out.

10% popularity Vote Up Vote Down


 

@Steve110

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.
toggleToc();
}
} 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:


`window.addEventListener('DOMContentLoaded',


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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme