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

231
Vistas
Call functions from different .js documents

I have 2 functions of 2 types of different .js documents.

File1.js

$(document).ready(function Function1(){
    var $input = $("#input-country");
    $input.typeahead({source:['United States', 'China', 'Germany']});
});

File2.js

$(document).ready(function Function2(){
    var $input = $("#input-country");
    $input.typeahead({source:['Menu 1', 'Menu 2', 'Menu 3']});
});

I created a global js, which called the 2 functions, global.js

Global.js

$(function TestName() {
    Function1();
    Function2();
});

When importing the files, only what is in the order works.

<script src="js/File1.js"></script>
<script src="js/File2.js"></script>

In this case, only File1.js works, the second does not.

or

<script src="js/File2.js"></script>
<script src="js/File1.js"></script>

In this case, only File2.js works, the first does not.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First of all, you should try to avoid polluting the global name space where possible, however, shared functionality in a web browser can be achieved by assigning to the window object as follows:

// File 1 - using an immediately invoked function expression (IFFE)
(function(global) {
  // make available globally:
  global.funcOne = myFunc;

  var privateVariable = 1;

  function myFunc() {
    return privateVariable;
  }
}(window));

// File 2 - using an immediately invoked function expression (IFFE)
(function(global) {
  // make available globally: 
  global.funcTwo = myFunc;

  var privateVariable = 2;

  function myFunc() {
    return privateVariable;
  }   
}(window));

// your "global" js file (using the jquery document ready wrapper):
$(function() {
   console.log(funcOne() + funcTwo()); // returns 3
});

Here is a JsFiddle which demonstrates this concept.

Note: The IFFE pattern is less common in as the latest release of ECMAScript along with a module bundler allows us to do this more elegantly.

To show this working for your specific example, here is what I would do:

// File 1
(function(global, $) {
    // make available globally:
  global.setCountries = setCountries;
  // private variable:
  var data = {source:['United States', 'China', 'Germany']};
  function setCountries() {
    $("#input-country").typeahead(data);
    console.log('setCountries() Called!'); // not required.
  }
// dependencies:  
}(window, jquery));

// File 2
(function(global, $) {
    // expose global function:
  global.setMenu = setMenu;
    // private variable:
    var data = {source:['Menu 1', 'Menu 2', 'Menu 3']};
    // private function:
  function setMenu() {
    $("#input-country").typeahead(data);
    console.log('SetMenu() Called!'); // not required.
  }
// dependencies:
}(window, jquery));

// Global Js
$(function() {
   function TestName() { 
       setMenu();
       setCountries();
   }
   TestName();
});
about 4 years ago · Santiago Trujillo 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