Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Find the src Attribute of a Frame Element


View products that this article applies to.

Summary

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

↑ Back to the top


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)) = "HTMLFrameElement" 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.

↑ Back to the top


References

For a Microsoft Visual Basic .NET version of this article, see 311292.
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
167796 PRB: Permission Denied Error Message When Scripting Across Frames
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB297950, kbwebbrowser, kbhowto, kbdhtml

↑ Back to the top

Article Info
Article ID : 297950
Revision : 3
Created on : 5/11/2006
Published on : 5/11/2006
Exists online : False
Views : 328