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

189
Visualizações
How to make script execute once the first script is loaded

I'm trying to make a web framework and one feature will be a key-value state management tool. I need the second <script> tag to only run after ./script.js loads in.
index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./framework.js"></script>
</head>
<body>
  <p f-text="name"></p>
  <script>
    Framework.store('name', 'Joe');
  </script>
</body>
</html>

framework.js:

document.querySelectorAll('*').forEach((element) => {
  if (element.hasAttribute('f-text')) {
    const textValue = element.getAttribute('f-text');

    const key = window.fStore.find((x) => x.key === textValue);

    element.innerHTML = key.value;
  }
});

window.Framework = {
  store: (key, value?) => {
    if (!value) {
      const foundKey = window.fStore.find((x) => x.key === key);

      return foundKey.value;
    }

    window.fStore = [...window.fStore, { key: key, value: value }];
  }
}

Error:

SyntaxError: Unexpected token ')'
    at /framework.js:12:22
ReferenceError: Framework is not defined
    at /:12:5
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to wait that your script is loaded, you can use this

window.addEventListener('load', function() {
    Framework.store('name', 'Joe');
})
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