7
I’m having a problem making a Function wait the loading of 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 find a way to do the functions on the page and wait for the loading of the package.js, I found some solutions that worked on FF and IE, most in the Chrome sometimes doesn’t work depending on the loading order.
what I have tried:
document.getElementById('packjs').addEventListener("load", chamoUmaFuncaoAqui,false);
It would be the perfect solution, and it works on FF and IE, but in the Chrome, I don’t know why. Sometimes this event doesn’t work and activates the function right at the beginning, so before the.js package. I tried to combine it with a var statement at the end of the.js package and then check if it exists,
if (typeof pronto !== 'undefined') {
chamoUmaFuncaoAqui();
}
but it didn’t work either.
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 charging is too fast, the time difference between the function and the event window.addEventListener
load is thousandths of seconds, and if it runs later, it doesn’t work.
my page with that test and some of the options I’ve tried. I left everything with console.log
to keep up with the problem.
I do not have such a great knowledge en javascript, but I believe that the problem has occurred only in Chrome when he for some reason (I think wrongly) gives the window.addEventListener
Domcontentloaded before loading the.js file (this does not occur in other browsers)
Any know how to get around this? If it can be with example thank you.
Solved, (I think, rs). before calling the.js file I added:
var pronto = false; var feito=false;
from within the.js file added: Roint=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 yet been called from within the.js file
if (pronto==true && feito==false)
{feito=true;executa('Chamado pela PAGINA ');}
But what he decided to do was create a condition inside the Function executes to check if the Document.addeventlistener "Domcontentloaded" already happened or not. That’s because in async Chrome the file might happen to be uploaded before that event, it didn’t seem to occur in FF and IE. So on the page it was 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 summary for what is pertinent, but basically that’s it and it worked. Thank you for your attention
Behold setTimeout/setInterval, setTimeout only runs once, it would not be necessary to create a loop?
– Guilherme Lautert
In my Chrome seemed to work normally your callback. But try this:
document.getElementById('packjs').onreadystatechange = function() { if (script.readyState == 'loaded') { console.log("CARREGOU") } };
. I wouldn’t be monitoring with timers.– bfavaretto