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

225
Visualizações
Drupal 8 - I cannot call a function in another JavaScript file

I have two JavaScript files on my custom theme. One file has functions that need to be used on the other file. What I have below doesn't work. This is the error I get.

Uncaught ReferenceError: test is not defined.

jQuery will be used on both files.

file1.js

/**
 * @file
 * UI behaviors
 */

(function ($, Drupal) {

 'use strict';

  $(document).ready(function () {
    function test(){
     console.log('hello'):
    }
  });

})(jQuery, Drupal);

file2.js

/**
 * @file
 * UI behaviors
 */

(function ($, Drupal) {

  'use strict';

  $(document).ready(function () {
     test();
  });

})(jQuery, Drupal);

customtheme.libraries.yml

scripts:
  js:
    js/file1.js: {}
    js/file2.js: {}

customtheme.info.yml

libraries:
 - 'customtheme/scripts'
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your test function is not in the global scope.
If you want your test function to be able to be called from anywhere, don't put it in the $(document).ready or in the IIFE.
ie. in the first file, just have..

function test(){
  console.log('hello);
}

You may also want to namespace your functions, but that is a separate question I suppose.

Also, for Drupal, you may be wanting to use drupal.behaviors rather than $(document).ready (for the second file)

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