Call function js in another file - Dependency between Javascript scripts

3

I'm in doubt on js to call a function in another js.

Example:

if (nome == ""){
   exibirModal();
}

Since the function " exibirModal() " is in another js, it is giving error as if the function had not been declared.

In my html I put the script exibirMensagem.js and then my cadastro.js

    
asked by anonymous 23.07.2014 / 21:37

5 answers

4

From what you're saying, if the files are being loaded in the correct order - that is, the one containing the exibirModal function declaration being loaded before you try to call this function -, everything indicates that you has a scope problem .

Probably the exibirModal function is being declared inside another function. If it does not depend on variables declared in that other function , move it out (ie for global scope) must resolve. Otherwise, change the declaration to something like:

function exibirModal() { ...

To:

window.exibirModal = function() { ...
    
24.07.2014 / 00:58
2

You can make the first script execute an existing function in the second script, but to do so, you should trigger the execution of the onload event of the document like this example (using jQuery):

$(document).ready(function() {
   somar(a,b); //somar() ainda será definido
});
    
23.07.2014 / 21:41
2

You should expect the entire page is loaded to access the DOM elements, which probably happens inside your function exibirModal() . So, try to take advantage of the call on a function triggered by the onload event:

window.addEventListener("load", function(){
    if (nome == ""){ exibirModal(); }
});
    
23.07.2014 / 21:42
2

Do you already know the RequireJS? link

RequireJS is a loader of JavaScript files and modules. It's a javascript library.

Download at link or refer to your html on the CDN //cdnjs.cloudflare.com/ajax/libs /require.js/2.1.14/require.min.js

With RequireJS you are informed that some of your javascript code requires, ie you need another javascript library to be loaded.

Example (adapting from link ):

Let's say your site has this structure:

project-directory/
--project.html
--scripts/
----cadastro.js
----exibirMensagem.js
----require.min.js  (faça o download em http://requirejs.org/docs/release/2.1.14/minified/require.js )

Then in your HTML do something like:

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- O atributo data-main diz para o require.js carregar
             scripts/cadastro.js depois que o require.js seja carregado. -->
        <script data-main="scripts/cadastro" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

Finally inside your cadastro.js you do something like:

    require(["exibirMensagem"], function(exibirMensagem) {

     if (nome == ""){ exibirModal(); }

    //Essa função será executada quando exibirMensagem.js for carregado.
    //Se exibirMensagem.js usar o método define() (mais detalhes em http://requirejs.org/docs/api.html#define ), então essa função só será disparada quando todos os módulos definidos forem carregados.
});

I hope it helps.

    
23.07.2014 / 23:22
0

Verifies that the order of the embedding of your script displayMessage.js and register.js. Open the browser console (usually F12) and try to perform the desired function.

    
23.07.2014 / 21:43