Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
JavaScript: Using HTML files to make components?

Is there a way to make files like navbar.html and then using JavaScript to use files like these as components? For example, Let's say I have a file called imageCarousel.html and I have to use this file in many different HTML files so how can I make a JavaScript file that has a function that takes in the class of where I want to display the HTML file's code, and then the path of the HTML file and then also how can I use that JavaScript file in other JavaScript files?

I have searched many different places and I can clearly state that there is no other place for this exact question. And I am asking this question after doing all the research.

Also I don't want to use any library here like react etc.

Thanks for reading this query!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can create your templates using JS functions. As explained in the comment, you would need some kind of development environment to write your code into multiple file and then organise them.

If you don't want to this...Here is what you can do.

index.html

<script src="/path/to/Navigation.js"></script>
<script src="/path/to/index.js"></script>

in Navigation.js

function Navbar (text) {
  return `
    <nav class='navbar'>
      <h1>Hello Template</h1>
      <a href="www.google.com">Go To Google</a>
      <h3>${text}</h3>
    </nav>
  `;
};
in index.js
document.getElementById("app").innerHTML = Navbar("Hello from template");

See this codesandbox for example: https://codesandbox.io/s/elegant-mccarthy-t6msj?file=/index.html

PS: This is a bad practice though, and it will get complicated as your application grows. Because the sequence of imported files will matter. Consider them as depedencies. In this case, index.js needs Navigation.js and this chain will grow with as many as components and scripts you add.

So I would suggest you to explore the module building library if you want to build an application. If you are just trying to learn...then I guess this will help.

about 4 years ago · Juan Pablo Isaza Relatório

0

With JS projects normally use modules in many .js files, not html. For import other modules can use import something from './file.js or const module = require('./someFile.js').

For bundle all files together and generate HTML + JS static files need to use bundlers. Bundlers also can transpile newer JS syntax for support older browsers.

Most used bundler is webpack, can look at rollup, parcel or esbuild too.

Have many tutorials how to use webpack

UPD. Can also look into RequireJS, but it's old approach.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you can do it with a js library like react.js ! with it you can create a js component and call it on other js files! if you dont want to use react that i dont know if its possible with pure js!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda