Skip to content

Commit c33b224

Browse files
Add option to load TOC accordion on load or after document has been loaded.
1 parent d953f2b commit c33b224

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

assets/accordion.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
document.addEventListener( 'DOMContentLoaded', () => {
1+
/**
2+
* SimpleTOC load function.
3+
*/
4+
const simpletocLoad = function() {
25
const buttons = document.querySelectorAll( 'button.simpletoc-collapsible' );
36

47
buttons.forEach( ( button ) => {
@@ -17,4 +20,17 @@ document.addEventListener( 'DOMContentLoaded', () => {
1720
: '0px';
1821
} );
1922
} );
20-
} );
23+
}
24+
25+
// Allow others to call function if needed.
26+
window.simpletocLoad = simpletocLoad;
27+
28+
// Check to see if the document is already loaded.
29+
if ( document.readyState === 'complete' || document.readyState !== 'loading' ) {
30+
simpletocLoad();
31+
} else {
32+
// Fallback event if the document is not loaded.
33+
document.addEventListener( 'DOMContentLoaded', () => {
34+
simpletocLoad();
35+
} );
36+
}

0 commit comments

Comments
 (0)