In my Firefox extension I am using DOMContentLoaded
to detect page load and insert my HTML. Is there an event which triggers before this and still the document is available at that time?
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A different event, load , should be used only to detect a fully-loaded page.
If you want to know "exactly" if DOMContentLoaded was fired, you could use a boolean flag like this: var loaded=false; document. addEventListener('DOMContentLoaded',function(){ loaded=true; ... }
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.
DOMContentLoaded does not wait for stylesheets to load, however deferred scripts do wait for stylesheets, and DOMContentLoaded queues behind deferred scripts. Also, scripts which aren't deferred or async (e.g. <script> ) will wait for already-parsed stylesheets to load.
Note: This answer refers to XUL-based extensions. As of Firefox 57, this technology is obsolete. The functionality mentioned here is no longer available to extensions.
There is content-document-global-created
notification that is sent out when a document is created, before any content is added to it (to be precise, it happens when the browser receives the HTTP headers of the response and knows that it isn't a redirect or such). That's the earliest point where you can get the document. The DOMContentLoaded
event is fired once Gecko finishes downloading the contents of the document, that's the earlies point where you can access the complete DOM. In between there is a bunch of other events, e.g. lots of progress listener events - which one you use depends on what you are trying to do, there is no general answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With