+ 1

How do I make the same menu appear on every page of my website?

For a website I'm making, I want to include the same menu on every webpage. It gets annoying to constantly copy and paste it into each HTML document and it forms a lot of clutter when reading the code. What is a good way to add it using a script? I hear I might be able to use PHP or a JavaScript library?

19th Jul 2017, 8:35 AM
James
James - avatar
2 Answers
+ 10
I always use php to do this. You can put the code of menu in a text/html file and use include function on php. <?php include ("menu.txt"); ?> This way it be included as written just by one line of code. PS:Remember that you will need to have a local/online server to test php.
19th Jul 2017, 8:43 AM
Farshad
Farshad - avatar
0
I tried raw JavaScript, and it works, but my code seems messy. It also stop ssome features of the webpages from working. var menuHTML = new XMLHttpRequest(); menuHTML.onreadystatechange = function() {   if (this.readyState == 4 && this.status == 200) {    var menuCode = menuHTML.responseText;    document.getElementById("layout").innerHTML += menuCode;   } }; menuHTML.open("GET", "menu.html", true); menuHTML.send();
19th Jul 2017, 8:35 AM
James
James - avatar