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

78
Vistas
Variable From Other Script Sometimes not Loading

I have Two JavaScript scripts linked to my HTML File in this order:

<script src="first.js"></script>
<script src="second.js"></script>

first.js has a variable which is accessed by second.js. The Variable is Used by second.js at the end of the file and the variable is defined in first.js at the beginning. Whenever I reload the site, a console error occasionally comes up saying that the Variable in first.js is not defined.

Uncaught ReferenceError: dbis not defined at second.js:80

Why is this? Thanks in Advance.

Full Example:

first.js:

var db = [{name: "Obj1", property: "property1"}, {name: "Obj2", property: "property2"}]; // At Line 8

second.js:

function loadDbItems() {
    for(i = 0; i < db.length; i++) {
        console.log(db[i].name); // Line is Near End of Script
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can register the variable with window to make it global guaranteed.
See script example below.

You should not pollute the window object with every little variable you need, but this way the variable will exist across scripts, provided the first one executes first.

window.db = [{name: "Obj1", property: "property1"}, {name: "Obj2", property: "property2"}];
<html>
  <body onload="loadDbItems()">
    <script>
      function loadDbItems() {
        for(var i = 0; i < window.db.length; i++) {
            console.log(window.db[i].name);
        }
      }
    </script>
  </body>
</html>

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