0

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)

Owyn
  • 663
  • 2
  • 8
  • 17

2 Answers2

1

If you want to run something after all the "texts" as you call it are loaded but before other js, you need to run all your js at the very bottom of the page (that is, btw, proposed best practice) and run anything you need ot be ran first at the very top...

Note that js is highly concurrent - meaning that lots of stuff is happening simultaneously, so I recommend you wrap your first code to run in one anonymous function, and everything else in the other, so it gets executed first.

Your document.ready can also be in anon f.

Rastko
  • 477
  • 2
  • 7
  • That sounds like a good idea, but I'm running my code from userscript space, - how can I fire an event in javascript so my userscript would pick it (hook it) and execute code from its own space (not documents one) ? – Owyn Aug 31 '13 at 12:34
  • If you give mi sample code in jsfiddle maybe I can give you some additional idea... In general you should, in first anon, extend specific userscript space for desired method and run specific method you just created right after wards... It does its thing and is immediately removed from the global scope after anon f is finished... – Rastko Aug 31 '13 at 12:44
0

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

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • So I should stick with my method(code) posted above? (cuz DOMContentLoaded might be fired in 10+ secons after page open cuz of scripts junk and code above fires almost right away) – Owyn Aug 31 '13 at 12:21
  • @Owyn: No, because the script that you are using doesn't do what you ask for. The `html` element has an ending tag before the whole document is loaded, it gets the ending tag from start. (Elements are single objects, not separate objects for the starting and ending tag, so when you get the OuterHTML for the element you always get the ending tag.) The reason that the DOMContentLoaded events fires so much later is that your current code fires way too early. That is also the reason that it fires multiple times, that is every time the document is updated from the time when it was created. – Guffa Aug 31 '13 at 12:45
  • I see, so for normal situations I should just use settimeout with few ms instead and for firefox+noscript users (settimeout doesn't work for them) leave running on every mutation (and check if document has enough contents I need) but remove the check for – Owyn Aug 31 '13 at 12:53
  • @Owyn: If you want to run the script when the page has loaded, you should simply use the DOMContentLoaded event. If you want to run the script when a specific part of the page has loaded, you should simply put the script inline after that part. – Guffa Aug 31 '13 at 14:02
  • I want to run it when the page has loaded but not its scripts because those may waste a lot of time – Owyn Aug 31 '13 at 16:17
  • @Owyn: That is not possible. When the browser encounters a script tag it will load and run the script before continuing to parse the rest of the page, so the page doesn't load before the scripts run. – Guffa Aug 31 '13 at 16:19