Microsoft KB Archive/297950

= How To Find the src Attribute of a Frame Element =

Article ID: 297950

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer (Programming) 5.01 SP1
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q297950



SUMMARY
This article demonstrates ways to programmatically find the src attribute of a frame element.



MORE INFORMATION
For a Web browser control host or an application that is automating Internet Explorer, you may want to programmatically find the src attribute of an IHTMLFrameElement. The following code samples provide a way to accomplish this in both Microsoft Visual Basic (VB) and Microsoft Visual C++ (VC).

Visual Basic:

Assuming that you have an instance of a WebBrowser control named WebBrowser1 and you have already browsed to a frameset page, you can use the following code to print out the src attribute of the frames.

NOTE: You must set a reference to the Microsoft HTML Object Library (Mshtml.tlb) and both Microsoft Internet controls (Shdocvw.oca and Shdocvw.dll). Dim fDoc As HTMLDocument Dim fra As SHDocVw.InternetExplorer Dim x As Integer

Set fDoc = WebBrowser1.Document For x = 0 To fDoc.All.length - 1 If TypeName(fDoc.All(x)) = &quot;HTMLFrameElement&quot; Then Set fra = fDoc.All(x) Debug.Print fra.Document.location.href End If    Next Set fDoc = Nothing Set fra = Nothing In VB, you can also use the HTMLWindow object. This object exposes the frameElement property, which you can then use to get the src attribute. However, this method only works with Internet Explorer versions 5.5 and later, and no cross frame scripting exists. Dim fDoc   As HTMLDocument Dim fra    As HTMLFrameElement Dim frawnd As HTMLWindow2 Dim x      As Integer

Set fDoc = WebBrowser1.Document '  Loop through the frames. If fDoc.frames.length <> 0 Then For x = 0 To fDoc.frames.length - 1 Set frawnd = fDoc.frames.Item(x) Set fra = frawnd.frameElement Debug.Print fra.src Next x   End If

Set fDoc = Nothing Set frawnd = Nothing Set fra = Nothing Visual C++:

In VC++, you can QueryInterface for the IHTMLDocument2 interface with an IWebBrowser2 interface. After you have a pointer to the document, you can loop through the ALL collection. For each element in the collection you can get the IHTMLFrameBase interface, and then call the get_src method to ask for the src attribute. HRESULT        hr = NULL; IDispatch*     pDisp = NULL; IHTMLDocument2* pDoc = NULL;

pDisp = m_webOC.GetDocument; if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)) && (pDoc)) {   IHTMLElementCollection* pColl = NULL; if(SUCCEEDED(hr = pDoc->get_all( &pColl ))) {      long length = 0; if(SUCCEEDED(hr = pColl->get_length(&length)) && (length)) {     for (int x = 0; x < length; x++ ) {        VARIANT    Index; IDispatch* pDisp2 = NULL; Index.vt = VT_UINT; Index.lVal = x;        VARIANT    var2; VariantInit( &var2 ); if(SUCCEEDED(hr = pColl->item (Index, var2, &pDisp2))) {       IHTMLFrameBase* pBase = NULL; if(SUCCEEDED(hr = pDisp2->QueryInterface(IID_IHTMLFrameBase,(void**)&pBase)) && (pBase)) {          BSTR src = NULL; pBase->get_src(&src); }         }       }    }      }   } In VC++, you can also loop through the ALL collection. For each element in the collection you can get the IHTMLElement interface and then call the getAttribute method to ask for the src attribute. However, this is not efficient because you can get a src attribute for many other elements (such as IMG, IFRAME, FRAME, XML DOC, and so on). If you use this method, you must determine whether the element is a frame by checking the IID_IHTMLFrameBase interface.

