Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Running a js function in html while the script tag has a defer attribute set

I the defer attribute of the script tag in the <head> section of HTML files. This almost always means that I cannot run js functions in the HTML since the js file is only loaded after the HTML file has been loaded..

I have run into a situation where I needed to run a js function in my HTML file, but would not want to remove the defer attribute from the script tag because of other functions in the js file. I looked at the possibility of using async, but it also was not helpful.

Any idea on how this could be done... other than using defer? I would still like to keep the script tag in the head, but going that way would mean that I have no open (non-function based) instructions in the linked js file... which is not always ideal.

Here is a sample of the HTML file

temp.html

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="temp.js" defer></script>
    <title>Defer et al</title>
</head>

<body>
    <script>
        runThisFunction('Home');
    </script>
</body>

</html>

And here is a sample of the js file

temp.js

function runThisFunction(varin) {
    console.log(varin);
}

If I remove the defer from the js file, the function will run; if I leave it there, or use async instead, the function will not run because it has not loaded yet.

Any assistance in this small issue will be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The best way to do this would be to refactor your script to contain an entry point in a function, and run that function once the DOM loads. Instead of, for example

// temp.js

function runThisFunction(varin) {
    console.log(varin);
}

document.querySelector('.foo').textContent = 'doing stuff';
// other code here

put all of the functionality that depends on the page being loaded into its own function:

// temp.js

function runThisFunction(varin) {
    console.log(varin);
}

function init() {
  document.querySelector('.foo').textContent = 'doing stuff';
  // other code here
}
window.addEventListener('DOMContentLoaded', init);

This way, if you also remove the defer attribute, runThisFunction will be available immediately, but the other parts of your code won't run until the page is loaded.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda