I’m not new to programming, but I am somewhat new to web development and I’m trying to figure out the most preferred way of taking a standalone header from one html document and adding it to other html documents without code duplication. If possible I want to do this with Javascript so I can learn with more basic tools before expanding further.

I’ve researched this a fair bit, but the advice is a bit confusing since it either seems out of date or possibly not the most secure way of handling things. Is there a preferred way of doing something like this?

    • MossBear@lemmy.worldOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      11 months ago

      I’m not far enough along to know how to answer this. I was under the possibly mistake impression that I could create a standalone html file with reusable components, tag them by id, get those elements by id in Javascript, store them as a variable and instance those component into another html document with an ordinary javascript file by getting a place holder div id in the main html file and adding the content using the innerHTML property.

      Is that not the case?

      • silas@programming.dev
        link
        fedilink
        English
        arrow-up
        4
        ·
        11 months ago

        If I understand you right, the closest thing to this natively is probably web components. They have really good support across browsers now, and they would accomplish what you want without adding extra javascript to weigh your site down.

        You could also learn and use a javascript framework like Astro, Svelte, Vue, React, etc. which are all industry-standard frameworks built to break your website down into a ton of reusable components and keep things organized. I like Svelte or Astro because they feel closer to vanilla HTML/CSS/JS to me. Here’s the official interactive tutorial for Svelte if you want to mess around with it: learn.svelte.dev

        PHP does have “includes” too if you want to go that route

        • MossBear@lemmy.worldOP
          link
          fedilink
          arrow-up
          2
          ·
          11 months ago

          I was looking into Astro and Svelte and those definitely seem interesting. I’ve saved links so I can get into them more in a few weeks when I’m a bit further along with the fundamentals. Thanks!

          • jaredwhite@kbin.social
            link
            fedilink
            arrow-up
            3
            ·
            11 months ago

            Also just throwing this out there: I run a Discord called The Spicy Web that really is about learning and building stuff with the fundamentals, even for old-timers like myself (but all the more for newbies! So much advice out there is about pulling in tons of opinionated tools and dependencies, even when you don’t need them…). At any point if you want to bounce ideas or questions off folks in real-time, check it out!

            • MossBear@lemmy.worldOP
              link
              fedilink
              arrow-up
              2
              ·
              11 months ago

              Much appreciated, thanks! I’ll save a link to this and have a look! I am definitely a fan of using simple tools, so that definitely appeals to me.

          • silas@programming.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            11 months ago

            No problem! Svelte has an awesome Discord community, and we got a Svelte community here too. Feel free to ask me any questions as well

      • William@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        11 months ago

        It’s technically possible to do that, but it’s kind of a pain. I asked about server-side JS because the server is where I’d do something like this as my first choice.

        If you really want to do it in the browser, you could use an AJAX call to get the html from the server, then use DOM functions to find that snippet by id. (Or just put them in separate html files and save yourself the pain of those DOM functions.)

        I found this for you. https://gomakethings.com/getting-html-with-fetch-in-vanilla-js/ I think it actually has most of what you want to do.

        • MossBear@lemmy.worldOP
          link
          fedilink
          arrow-up
          2
          ·
          11 months ago

          Thanks for the link and the advice. I wasn’t sure how to approach something like this since it’s handled a bit differently with the software development I’m used to, but I think I’m starting to get a clearer idea of why things work the way they do on the web side.