I am developing a website and I created a js script that creates side menu for me.
It reads all the headings and creates scroll menu.
It works perfectly the problem that I am facing now is that I have to call the js function on every post page. Since I'll have lots of posts on my web page this could be a bit time consuming. Any way to make my script load automatically every time a post is being shown on web page?
My code if anyone is interested is bellow:
function create_navigation() {
allid = []; //tabela z vsemi id ji, ki morajo biti v meniju
elements = document.getElementsByTagName("h1");
for (var i = 0; i < elements.length; i++) {
if (elements[i].id != "") {
allid.push(elements[i].id)
}
}
elements = document.getElementsByTagName("h2");
for (var i = 0; i < elements.length; i++) {
if (elements[i].id != "") {
allid.push(elements[i].id)
}
}
for (var i = 0; i < allid.length; i++) {
var table = document.getElementById("qweqwe");
var row = table.insertRow(i);
var cell1 = row.insertCell(0);
cell1.innerHTML = "<a href=\"#" + allid[i] + "\" class=\"_ps2id _mPS2id-h mPS2id-highlight mPS2id-highlight-first mPS2id-highlight-last\" data-ps2id-offset=\"\">" + document.getElementById(allid[i]).innerHTML +
"</a>";
}
}
Currently I call the function create_navigation on every post page that I want it to appear.
Also is this the correct way of doing this? I am kinda inexperienced in Wordpress and had time coming with better idea of dynamically creating side menu.