Search code examples
javascriptmutation-observersdomcontentloaded

javascript event: get when document is textually fully loaded but scripts haven't ran yet


DOMContentLoaded fires after document is loaded and its scripts ran, and I'd like to run my code before scripts were executed but after document.body.outerHTML ( document.childNodes[document.childNodes.length-1].outerHTML ) is full

I try this, but looks like it runs many times after is found also so now I do a timer 200ms job before executing mycode, but i'd like to do it without timers

var observer = new MutationObserver(function(mutations) 
{
    if(document.body && document.childNodes[document.childNodes.length-1].outerHTML.lastIndexOf("</html>")!=-1)
    {
        observer.disconnect();
        mycode();
    }
});
observer.observe(document, {subtree: true, childList: true}); 

Needs to work in Chrome (so beforescriptexecuted event won't work here)


Solution

  • You can't do that. Scripts are executed immediately, so they run before the document is fully loaded.