Problem calling javascript function

1

I'm doing a web application with javascript, my js file has several CRUD functions, I wanted it when HTML was loaded, called two functions, so I added this code:

document.addEventListener('load', function() {
    ler() //le os dados do firebase
    mostrar() //mostra esse dados formatados
})

But it does not work, what's the problem?

Edit:

As you gave the hint, I used window instead of document and I put alert() , for testing and it worked ( alert() ), so I guess something else is causing the problem

Here is the code for ler() and mostrar() :

function ler() {
    json = []

    firebaseRef.once("value")
        .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key
            var obj = childSnapshot.val()

            obj.key = key
            json.push(obj)

        })
    })
}

function mostrar() {
    receitas.innerHTML = ""

    for(var i = 0; i < json.length; i++) {
        receitas.innerHTML +=   '<div class="col s12 m4 l3">' +
                                    '<div class="card">' + 
                                        '<div class="card-image">' +
                                            '<img src="./image/default.png">' +
                                            '<span class="card-title">' + json[i].nome + '</span>' +
                                            '<span class="hide">' + json[i].key + '</span>' +
                                        '</div>' +
                                    '</div>' +
                                '</div>'
    }
}

Both work by calling functions in the console

    
asked by anonymous 29.01.2018 / 01:01

4 answers

1

I solved the problem by changing the function mudar() , instead of looping to show the data it received two parameters (key and name) and called the function passing the values instead of adding in a json p>     

29.01.2018 / 01:49
1

You can load the functions this way:

window.load = ler(), mostrar();
    
29.01.2018 / 01:04
1

The load event may sometimes not fire at document . Instead, use window :

window.addEventListener('load', alert('I am working!'));
    
29.01.2018 / 01:06
0

Change load by DOMContentLoaded :

document.addEventListener("DOMContentLoaded", function() {
    ler() //le os dados do firebase
    mostrar() //mostra esse dados formatados
})

More info about the this MDN documentation .

function ler(){
   console.log("ler");
}

function mostrar(){
   console.log("mostrar");
}

document.addEventListener("DOMContentLoaded", function() {
    ler() //le os dados do firebase
    mostrar() //mostra esse dados formatados
})
    
29.01.2018 / 01:47