How To Use VBScript to Control the Contents of Another Frame

ID: Q156315

The information in this article applies to:

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:

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 
Version           : 1.00 1.10 2.00
Platform          : WINDOWS

Last Reviewed: July 2, 1999