0

I'm just coding my first Website and it has lots of Subsites. Every Website needs the same code-block at some point. This code block just contains HTML code. Is it possible to make a data with this code-block and import it on every website? My goal is: If I have to update the code-block I just want to do it once.

The code-block is a sidebar and looks like this:

<div id="sidebar">
    <div class="inner">
        <nav id="menu">
            <ul>
            <li><a href="../index.html">Homepage</a></li>
            <li>
                <span class="opener">SomeName</span>
                <ul>
                <li><a href="../Somestuff/SomeWebsite.html">Main Page</a></li>
                </ul>
            </li>
            </ul>
        </nav>
    </div>
</div>
MefAldemisov
  • 867
  • 10
  • 21
Titanlord
  • 143
  • 7
  • There are 1000 ways to do this. Use php, java or whatever server language you want to include files in another. – cloned Jul 10 '20 at 07:34
  • You literally just described the whole idea behind React.js. Might be daunting to someone just starting off but worth looking into. Link----> https://reactjs.org/ – StrayAnt Jul 10 '20 at 07:36

3 Answers3

2

This question could be useful. Also this post asks about similar things.

Also frameworks (such as Vue, React, Angular...) are used for such purposes.

MefAldemisov
  • 867
  • 10
  • 21
0

Which extensions are your file? Php or html? Cause if it’s php you can make a new file (for your example: nav.php) and then in every subsites you need to do: require path_to_your_nav.php

If it’s html, i dont know there is no solution yet

0

You could make a simple JS function that sets the innerHTML of a already existing HTML element. And put the function in an external JS file.

example below

<!DOCTYPE html>

<head>
    <title>Test</title>

    <!-- link external js file with create element function -->
   <script type="text/javascript">
        function createElement(){
            //Get parent container
            const parent = document.getElementById('sidebar');

            //Set inner html 
            parent.innerHTML = 
            `<div id="sidebar">
                <div class="inner">
                    <nav id="menu">
                        <ul>
                        <li><a href="../index.html">Homepage</a></li>
                        <li>
                            <span class="opener"SomeName</span>
                            <ul>
                            <li><a href="../Somestuff/SomeWebsite.html">Main Page</a></li>
                            </ul>
                        </li>
                        </ul>
                    </nav>
                </div>
            </div>`;
        }
   </script>
</head>
<body>
    
    <div id="sidebar">
        <script type="text/javascript">
            createElement();
        </script>
    </div>


</body>

</html>
StrayAnt
  • 369
  • 2
  • 7