Navigation: For Developers > Extend Functionality with HEScript > Sample Scripts >

How to dynamically modify the Table of Contents?





HTML Executable: Modifying the Table of Contents at Runtime 📚


Welcome to a comprehensive guide on how to modify the Table of Contents (TOC) in your HTML Executable ebook or publication at runtime using HEScript. This guide will walk you through the process in a clear and concise manner. Let's get started! 🚀


The Objective 🎯


This topic explains how you can show and hide TOC items at runtime. For instance, you might want to display some TOC items only if the publication works in Registered mode.


Associating HEScript Functions to TOC Items 📝


In the TOC editor, there is a property named "Visibility HEScript Function". This property can point to a Boolean HEScript function that defines whether the TOC entry is visible or not.



The syntax should be: `[scriptname].[booleanfunctionname]`


When the publication loads the TOC, if a TOC item is associated with a Boolean function, the latter is executed. If its result is True, the TOC item is displayed; otherwise, if the result is False, the TOC item is hidden.


To refresh the TOC, you can use the `RefreshTOC` HEScript built-in macro.


Example 🖥️


This sample hides TOC items if the publication works in Trial mode, and shows them if it is in Registered mode.


Step 1: Edit the UserMain Script 🖋️


Go to the Script Manager, edit the "UserMain" script and copy/paste the following code:


function HideItIfTrial: Boolean;
// Returns False to hide the TOC item if HEPubRegistered is set to 1 (Registered) 
Result := GetGlobalVar("HEPubRegistered", "0") = "1"; 


Step 2: Choose the TOC Item 📚


Close the script editor, go to the Table of Contents page and choose the TOC item you want. Enter the reference to the script function: `UserMain.HideItIfTrial` or select it with the "..." button. Press Enter to save changes.




Step 3: Enable the Trial Publication Option 🛠️


Make sure you have enabled the option to make a Trial publication and compile it.