I have a problem to make a function wait for loading the package.js with the async tag
<script id="packjs" async type="text/javascript" src="pacote.js"></script>
This file contains jQuery and some plugins. The problem is that when I load with async
I have to sort out a way to do the functions on the page and wait for loading the package.js, I found some solutions that worked in < strong FF and IE , but Chrome sometimes does not work depending on the loading order.
What I've already tried:
document.getElementById('packjs').addEventListener("load", chamoUmaFuncaoAqui,false);
It would be the perfect solution, and it works on FF and IE , but on Chrome , I do not know why. Sometimes this event does not work and activates the function at the beginning, so before the package.js. I tried combining with a var statement at the end of the .js package and then check if it exists,
if (typeof pronto !== 'undefined') {
chamoUmaFuncaoAqui();
}
But it also did not work.
function init() {
if (window.jQuery) {
// Código dependente do jQuery fica aqui
tempo=Date.now()-timestamp;
console.log('Alternativa Função init com setTimeout (se ocorrer depois de window.addEventListener load não funcionará): '+tempo);
//car_news();
//Car_Com();
} else {
window.setTimeout(init, 100);
}
}
init();
The problem is that when loading is very fast, the time difference between the function and the window.addEventListener
load event is one thousandths of a second, and if it runs later, it does not work.
my page with this test and some of the options I've tested. I left everything with console.log
to follow the problem.
I do not have such a great knowledge in javascript, but I believe the problem has only occurred in Chrome when it for some reason (I believe wrongly) gives window.addEventListener
DOMContentLoaded before loading the .js file (this does not occur in other browsers)
Does anyone know how to get around this? If you can be as an example, thank you.
Resolved, (I think, rs). before calling the .js file I added:
var pronto = false; var feito=false;
from within the .js file I added: ronto = true;
if (feito==false && typeof executa !== 'undefined')
{feito=true;executa('Chamado de dentro do Arquivo PACK.JS ');};
On the page I created a second call if it has not been called from within the .js file yet
if (pronto==true && feito==false)
{feito=true;executa('Chamado pela PAGINA ');}
But what solved it was to create a condition within the function executes to check whether the document.addEventListener "DOMContentLoaded" was already happening or not. That's because in Chrome with async the file might happen to be loaded before this event, it did not seem to occur to me in FF and IE. So, the page looks like this:
function executa(a){
if (DOMpronto==true) {
/*o que precisar ANTES do DOM ter sido carregado*/
}else{
/*o que precisar DEPOIS do DOM ter sido carregado*/
}
}
function evento(f) {
if (window.addEventListener)
{window.addEventListener("load", f,false);}
else if (window.attachEvent)
{window.attachEvent("onload", f,false);}
else
{window.onload = f;}
}
var DOMpronto=false
evento(function(){console.log('carregoru DOM ');DOMpronto=true})
if (pronto==true && feito==false) {feito=true;executa('pela PAGINA ');}
I gave a pro's summary which is pertinent, but that's basically it and it worked. Thank you for your attention.