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

179
Vistas
using functions across multiple scripts/functions

I have a handful of js scripts in jquery:

vars.js
 $(function(){
      function yesOrNo(val){
          if (val == "yes"){
              return "yes";
          }
          if (val == "no"){
              return "no";
          }
 });

scripts1.js


 /// include(vars.js) <-- what would go here?

   $(function(){
       
       var var1 = "yes";
       var test1 = yesOrNo(var1);
       alert(test1);
   });

scripts2.js

  
 /// include(vars.js) <-- what would go here?

   $(function(){

       var var2 = "no";
       var test2 = yesOrNo(var2);
       alert(test2);
   });

scripts3.js

 /// include(vars.js) <-- what would go here?

   $(function(){
       var var3 = "yes";
       var test3 = yesOrNo(var3);
       alert(test3);
   });

How can i call the function yesOrNo from a different scripts.js page?

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

0

If you want to add another file add type="module" parameters in your main code, then add the line import {yesOrNo} from './vars.js'; in your code. Then, in the vars.js file, add the function as export function yerOrNo, not function yesOrNo.

My edit of your code:

vars.js

export function yesOrNo(val){
  if (val == "yes"){
    return "yes";
  }
  if (val == "no"){
    return "no";
  }
}

main.js

import {yesOrNo} from './vars.js'
$(function(){
  var var2 = "no";
  var test2 = yesOrNo(var2);
  alert(test2);
});

On your html call main.js like that

<script src="main.js" type="module"></script>
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