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

183
Visualizações
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 Respostas
Responde à pergunta

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 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