Confluence TOC Macro Bug

I’ve just been doing some customisation on the Confluence wiki, and ran into a bug while using the old Documentation theme. I hacked this JavaScript to create a fix that works for me. It needs to go in the Custom HTML, in the ‘end of Body’ section.

The bug: https://jira.atlassian.com/browse/CONF-39958

The fix:

<script type="text/javascript">
// Make the hash links work in the toc macro.
// Workaround for this bug: https://jira.atlassian.com/browse/CONF-39960
// Place this in the Custom HTML. It has to be in the 'end of Body', not the
// head, so that the toc links are loaded.
AJS.toInit(function() {
    AJS.$('.toc-link').click(function() {
        var hash = AJS.$(this).attr("href");
        var target = AJS.$(hash);
        if (target.length > 0) { 
            AJS.$("#splitter-content").scrollTop(target.position().top) 
        }
    });
});
</script>

I’ve shared this with the bug thread, but copied it here because it is more nicely formatted. It might work for me but create problems for you, because of customisations I’ve done elsewhere. And also this won’t work on Confluence Cloud, because you cannot add custom HTML.