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

224
Visualizações
How to run a JS Function in HTML5

How can I run my function "title()" in HTML? I have created a "main.js" File, in there, there is following Code:

 "use strict";

document.addEventListener("DOMContentLoaded", function() {

    let newScript = document.createElement("script");
    newScript.src = "javascript/head.js";
    let heads = document.getElementsByTagName("head")[0];
    console.log(heads)
    heads.prepend(newScript);
});

in the "main.js" File load an another Script which is called "head.js" and here is the Code of this File:

function title(titleName) {
    let title = document.createElement("title");
    document.title = titleName;
    document.head.appendChild(title);
}

Maybye you need my HTML Code:

    <!DOCTYPE html>
<html lang="en">
<head>
    <script src="javascript/main.js"></script>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script> title("Framework"); </script>
</head>
<body>
    

</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

in head.js add:

window.addEventListener("load", () => title("my desired title name"));

Not sure why someone downvoted, but to clarify, you can run this code anywhere so long as the head.js file has been loaded, it doesn't have to be directly in head.js.

You can add it into main.js:

let newScript = document.createElement("script");
newScript.addEventListener("load", () => title("my desired title name"));
newScript.src = "javascript/head.js";
// ...

You can also run it directly in the HTML in a <script> tag but you'll have to ensure the script is loaded first.

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