سؤال

I want to listen to the DOMContentLoaded event in a BHO for Internet Explorer 9. I've attached the event to the IHTMLDocument3 in the NavigateComplete2 event without success (attachment goes fine, but the event does not trigger).

I've done something similar for onload: I've attached the event "onload" to the IHTMLWindow3. It works fine in this case.

I know that the DOM is not completely loaded when NavigateComplete2 triggers because I cannot access the BODY of the page at that time.

How can I capture DOMContentLoaded in a BHO?

هل كانت مفيدة؟

المحلول

I'm not sure why DOMContentLoaded isn't firing. The most logical explanation is that it's already fired by the time you handle NavigateComplete2, though it's a bit strange that you can't access the whole DOM in that case. What's the readyState of the document inside the handler?

Anyway, a better solution might be to register a sink for OnReadyStateChange, something like this:

class CDocumentSink :
  public IDispEventImpl<1, CDocumentSink, &DIID_HTMLDocumentEvents2, &LIBID_MSHTML, 4, 0>
{
public:
  BEGIN_SINK_MAP(CDocumentSink)
    SINK_ENTRY_EX(1, DIID_HTMLDocumentEvents2, DISPID_READYSTATECHANGE, OnReadyStateChange)
  END_SINK_MAP()

  STDMETHOD_(void, OnReadyStateChange)(IHTMLEventObj* ev);
};

You could implement this as part of an existing or make a separate class (as in the above example). When the readyState is 4 (complete) the DOM should be there and usable.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top