Search code examples
c++htmlinternet-explorerdomhead

Get HTML Head from IE DOM


I know how to obtain the HTML body of a document, using the IHTMLDocument2 interface (by calling the get_body member function.

But how can I get the head? There is no such function in the IHTMLDocument2 interface?


Solution

  •     CComPtr<IHTMLDocument2> htmlDocument;
        CComPtr<IHTMLElementCollection> elementCollection;
    
        htmlDocument->get_all(&elementCollection);
        for (long i=0;i<numberOfElements;i++)
        {
            _variant_t index = i;
            CComPtr<IHTMLElement> htmlElem;
            CComPtr<IDispatch> htmlElemDisp;
            hResult = elementCollection->item( index,index ,(IDispatch **) &htmlElemDisp );
            if (FAILED(hResult) || (!(htmlElemDisp)))
            {
                continue;
            }
            hResult = htmlElemDisp->QueryInterface( IID_IHTMLElement ,(void **) &htmlElem);
            if (FAILED(hResult) || (!(htmlElem)))
            {
                continue;
            }
            hResult = htmlElem->get_tagName(&buffer);
    
            if (FAILED(hResult) || (!(buffer)))
            {
                continue;
            }
            if (_wcsicmp(buffer,L"HEAD")==0) 
            { 
             // your code here
            }
    }
    

    Also, you can use IHTMLDocument2* htmlDocument instead of CComPtr<IHTMLDocument2> htmlDocument. The main idea is to obtain all elements within your document, iterate them and find the one that has the tagName HEAD. Hope this helps.