0

I've been working on my site for a while now, and I've been adding tons of sections to my site as time goes on, but the way I've been doing is that whenever I add a new button to redirect to a new section, I have to manually edit EVERY SINGLE PAGE individually, and although it's not exactly hard or takes too long, I'm not sure how well this is going to be in the long run

Here's he headers in question

Is there any way to make these elements consistent with every page by just changing a single file?

Here's my HTML code

<div class="logocontainer">
 <a href="index.html">
 <img src="images/badasslogo.png" align="center" class="logo"></a>
 </div>
<body>

<div class="buttoncontainer">
  <a href="index.html">
    <img src="images/buttons/homebutton.png" class="button"></a>

  <a href="blog/blogmain.html">
    <img src="images/buttons/blogbutton.png" class="button"></a>

  <a href="art/artmain.html">
    <img src="images/buttons/artbutton.png" class="button"></a>

  <a href="fanart/fanartmain.html">
    <img src="images/buttons/fanartbutton.png" class="button"></a>

  <a href="partners/partnersmain.html">
    <img src="images/buttons/partnersbutton.png" class="button"></a>
    
  <a href="guestbook/guestbook.html">
    <img src="images/buttons/guestbookbutton.png" class="button"></a>

  <a href="servers/serversmain.html">
    <img src="images/buttons/serversbutton.png" class="button"></a>
    
  <a href="downloads/downloadsmain.html">
    <img src="images/buttons/downloadsbutton.png" class="button"></a>
    
  <a href="extras/extrasmain.html">
    <img src="images/buttons/extrasbutton.png" class="button"></a>

  <a href="donate/donatemain.html">
    <img src="images/buttons/donatebutton.png" class="button"></a>
  
  <a href="about/about.html">
    <img src="images/buttons/aboutbutton.png" class="button"></a>
</div>

And here's the CSS

.logocontainer {
   text-align: center;
}

.logo {
   display: inline-block;
   margin-bottom: 10px;
}

.buttoncontainer {
    text-align: center;
}

.button {
    display: inline-block;
}
  • 1
    If you were to create the site using Hugo, it would be as simple as adding a `partial` which could be imported at the top of every page. In JS frameworks like Vue or React, that section could be it's own component and reused across all pages. In vanilla HTML, you could create a javascript file which renders the html elements, and have that imported and run on every page. Then you can update it once and it should automatically work across pages. Just a few ideas. BTW, nice buttons -- looks really cool! – rishimaharaj Sep 02 '22 at 05:37

2 Answers2

0

You need to add your menu to menu.html page. then call menu.html page via jquery.

menu.html

  <a href="index.html">
    <img src="images/buttons/homebutton.png" class="button"></a>

  <a href="blog/blogmain.html">
    <img src="images/buttons/blogbutton.png" class="button"></a>

  <a href="art/artmain.html">
    <img src="images/buttons/artbutton.png" class="button"></a>

  <a href="fanart/fanartmain.html">
    <img src="images/buttons/fanartbutton.png" class="button"></a>

  <a href="partners/partnersmain.html">
    <img src="images/buttons/partnersbutton.png" class="button"></a>
    
  <a href="guestbook/guestbook.html">
    <img src="images/buttons/guestbookbutton.png" class="button"></a>

  <a href="servers/serversmain.html">
    <img src="images/buttons/serversbutton.png" class="button"></a>
    
  <a href="downloads/downloadsmain.html">
    <img src="images/buttons/downloadsbutton.png" class="button"></a>
    
  <a href="extras/extrasmain.html">
    <img src="images/buttons/extrasbutton.png" class="button"></a>

  <a href="donate/donatemain.html">
    <img src="images/buttons/donatebutton.png" class="button"></a>
  
  <a href="about/about.html">
    <img src="images/buttons/aboutbutton.png" class="button"></a>

Index.html

<html>
<head>
    <title>abc</title>
    <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
</head>
<body>
    <div class="logocontainer">
        <a href="index.html">
            <img src="images/badasslogo.png" align="center" class="logo"></a>
        </div>
        <div class="buttoncontainer" id="buttoncontainer">

        </div>
        <script> <!-- Call this script on all page-->
            $(document).ready(function () {
                $('#buttoncontainer').load('menu.html');
            });
        </script>
    </body>
</html>
Umar Ali
  • 404
  • 4
  • 14
0

You can do this:

<div id="buttoncontainer">
</div>

<script src="./path-to/js/header.js"></script> // add before the closing body tag

and in your header.js you can do:

let headerContent = `
<a href="index.html">
    <img src="images/buttons/homebutton.png" class="button"></a>

  <a href="blog/blogmain.html">
    <img src="images/buttons/blogbutton.png" class="button"></a>

  <a href="art/artmain.html">
    <img src="images/buttons/artbutton.png" class="button"></a>

  <a href="fanart/fanartmain.html">
    <img src="images/buttons/fanartbutton.png" class="button"></a>

  <a href="partners/partnersmain.html">
    <img src="images/buttons/partnersbutton.png" class="button"></a>
    
  <a href="guestbook/guestbook.html">
    <img src="images/buttons/guestbookbutton.png" class="button"></a>

  <a href="servers/serversmain.html">
    <img src="images/buttons/serversbutton.png" class="button"></a>
    
  <a href="downloads/downloadsmain.html">
    <img src="images/buttons/downloadsbutton.png" class="button"></a>
    
  <a href="extras/extrasmain.html">
    <img src="images/buttons/extrasbutton.png" class="button"></a>

  <a href="donate/donatemain.html">
    <img src="images/buttons/donatebutton.png" class="button"></a>
  
  <a href="about/about.html">
    <img src="images/buttons/aboutbutton.png" class="button"></a>
`;
document.querySelector('#buttoncontainer').insertAdjacentHTML('beforeend', headerContent);

Call it on all pages, then it will apply all changes to all pages once you edit it in your js file.

RandomCoder
  • 395
  • 2
  • 10
  • This did the trick, thanks a ton! – June S. Sai Sep 02 '22 at 07:40
  • Quick question Whenever I add it to other pages, for some reason, even if I specifiy the path of the files, only one pages gets it applied For example; index.html works perfectly since it's on the root but the moment I put the scrip on the other pages (which are above one directory) no image shows up, even thought the script gets detected How do I fix this exactly? – June S. Sai Sep 02 '22 at 08:15
  • thats an issue with the root, it is correctly applies to the index because its within the correct paths, but if you have your other pages on different roots then the path changes to certain images. You can either do 1 of 2 things I guess.. change the root of all other pages to the exact same one of `index` or do 2 seperate js files with different paths one for index and 1 for all the other pages. – RandomCoder Sep 02 '22 at 08:28