javascript - listen loaded event of each resource (all scripts, css, images, etc) -


i working page loaded percentage bar based on every single resource in page loading: mean monitoring every image, or script, css, etc , use "loaded event" increase general page loaded percentage

i have readed in posts difficult monitor loaded event of elements, question how using javascript / jquery? strategy can use achieve result?

i tried this, not working well

function calc(element){     console.log(element.outerhtml);     progress++;     var loadingstatus = ( progress * 100 / totallength );     console.log(loadingstatus);     document.getelementbyid('loadingbar').style.width = loadingstatus+'%'; }  document.onreadystatechange = function(){     if(document.readystate == "interactive"){          progress = 0;         var styleelements = document.queryselectorall('style');         var linkelements = document.queryselectorall('link');         var scriptelements = document.queryselectorall('script[src]');         var imgelements = document.queryselectorall('img');         totallength = (styleelements.length + linkelements.length + scriptelements.length + imgelements.length);          for(var i=0; i<styleelements.length; i++){             styleelements[i].setattribute('onload','calc(this);');         }          for(var i=0; i<linkelements.length; i++){             var source = linkelements[i].href;             linkelements[i].remove();             var newlink = document.createelement('link');             newlink.setattribute('href', source);             newlink.setattribute('rel', 'stylesheet');             newlink.setattribute('onload','calc(this);');             document.head.appendchild(newlink);         }          for(var i=0; i<scriptelements.length; i++){             var source = scriptelements[i].src;             scriptelements[i].remove();             var newscript = document.createelement('script');             newscript.setattribute('src', source);             newscript.setattribute('onload','calc(this);');             document.head.appendchild(newscript);         }          for(var i=0; i<imgelements.length; i++){             imgelements[i].setattribute('onload','calc(this);');         }      } } 

actually problems code are:

  • resources errors (fail load, 404, etc) break percentage calculation
  • <style> elements ignored percentage calculation depending placed (like after <link> elements, or before <script> elements)
  • resources loaded css links not recognized

update: after tests started thinking useless, because removing , readding elements (like scripts) cause executed 2 times...

i thinking use document write scan source code, replace , stuff, not bad idea, maybe not possible because have perform @ end of body (to full page source, head + full body), when script , stuff been executed...

so.. not know do, maybe there no way create progress bar based on resources load, right?


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -