Event which triggers before DOMContentLoaded
Asked Answered
C

1

11

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?

Crackup answered 12/10, 2011 at 19:1 Comment(0)
E
7

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.

Etheridge answered 12/10, 2011 at 19:19 Comment(10)
I am using this notification. It works. But this notification is generated for iframes also. How can I find whether the notification is from iframe or not?Crackup
@SelvarajMA: How about event.target.top == event.target?Etheridge
I am using subject.parent.length == 0. That works. But body is not available when this notification is sent. Is there any solution for this. I tried using a clock which will check for availability for body once in every 10ms after this notification is received. But it doesn't seem to work. Is there any solution for this?Crackup
@SelvarajMA: The page Wladimir linked to also mentions 'document-element-inserted', which is fired when the root element is inserted into the DOM. Why do you need the body specifically?Jola
@Jola I need to do something like body.appendChild()Crackup
@SelvarajMA: How about document.documentElement.appendChild()?Etheridge
all the links in this answer are now broken.Imf
@rags2riches: That’s unsurprising given that they refer to obsolete functionality. As of Firefox 57, none of this functionality can be used by add-ons. Note the XUL tag, none of this is relevant any more…Etheridge
@WladimirPalant I am not surprised. It was just a heads-up for users like me who came across this post whilst browsing. So the links and tags are no longer relevant while the question, taken on a more general level for any browser context, still is.Imf
@rags2riches: I added a corresponding note to the answer. The problem is: there is a metric ton of answers referring to XUL-based extensions on this site, I alone have more than 200. Adding these notes manually isn’t practicable.Etheridge

© 2022 - 2024 — McMap. All rights reserved.