How To Use VBScript to Control the Contents of Another Frame

Last reviewed: March 5, 1998
Article ID: Q156315
The information in this article applies to:
  • Microsoft Visual Basic, Scripting Edition, versions 1.0, 1.1, 2.0
  • Internet Explorer (programming), versions 3.0, 3.01, 3.02, 4.0, 4.01

SUMMARY

This article describes three methods for using Visual Basic, Scripting Edition, to alter the contents of another frame in the currently displayed frameset. Following are the three methods:

  • Locate the frame and write to its document object directly.
  • Change the HREF that the frame displays using LOCATION.HREF.
  • Change the HREF that the frame displays using NAVIGATE.

NOTE: All of these methods involve replacing what may already be displayed on a frame. If you want to replace just a portion of what is displayed, then you have to use an ActiveX control, which is placed on that page and designed for that purpose.

Also, please note that in Internet Explorer 4.0x, you cannot script across frames for pages that are on separate domains. For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q167796
   TITLE     : PRB: "Permission Denied" When Scripting Across Frames"

MORE INFORMATION

The first method of changing the content is to use DOCUMENT.WRITE. This allows you to create a page instantly without having an HTML file stored on the server. Remember to use DOCUMENT.CLOSE after writing to the other frame.

The second way of changing the content is to modify the LOCATION.HREF property. Changing this property for another frame causes the frame to load an HTML file stored on the server.

The third and final way is to use the NAVIGATE method. This works the same as modifying the LOCATION.HREF property, but calls a method instead.

All of these methods need to address a different frame than the current one. The example below demonstrates two ways to accomplish this. This first is to use the FRAMES collection, and the other is to use the name of the frame you want to change. In both cases, the parent document needs to be addressed first using WINDOW.PARENT.

To demonstrate how this works, create the following HTML pages and browse DEFAULT.HTM in Internet Explorer 3.0 or later. When you click on the buttons in the left frame, the content of the right frame changes.

Default.htm:

   <FRAMESET COLS=50%,50%>
      <FRAME NAME=LEFT SRC=LEFT.HTM>
      <FRAME NAME=RIGHT SRC=RIGHT.HTM>
   </FRAMESET>

Left.htm:

   <HTML>
   <BODY>
   <INPUT TYPE="BUTTON" NAME="CMD1" VALUE="DOCUMENT.WRITE">
   <INPUT TYPE="BUTTON" NAME="CMD2" VALUE="LOCATION.HREF">
   <INPUT TYPE="BUTTON" NAME="CMD3" VALUE="NAVIGATE">
   <SCRIPT LANGUAGE=VBSCRIPT>
   <!--
      SUB CMD1_ONCLICK()
         WINDOW.PARENT.FRAMES(1).DOCUMENT.WRITE "<H1>New Text</H1>"
         WINDOW.PARENT.RIGHT.DOCUMENT.CLOSE
      END SUB

      SUB CMD2_ONCLICK()
         WINDOW.PARENT.FRAMES(1).LOCATION.HREF="NEW.HTM"
      END SUB

      SUB CMD3_ONCLICK()
         WINDOW.PARENT.RIGHT.NAVIGATE("NEW.HTM")
      END SUB
   -->
   </SCRIPT>
   </BODY>
   </HTML>

Right.htm:

   <HTML>
   <BODY>
   This is a blank page in the right frame.
   </BODY>
   </HTML>

New.htm:

   <HTML>
   <BODY>
   This is a new page.
   </BODY>
   </HTML>

Keywords          : PrgOtherVBScript kbhowto kbprg
Version           : 1.00 1.10 2.00
Platform          : WINDOWS


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.