How To Maintain State Across Pages with VBScript

Last reviewed: January 13, 1997
Article ID: Q157906
The information in this article applies to:
  • Microsoft Visual Basic, Scripting Edition, versions 1.0, 1.1, 2.0

SUMMARY

This article illustrates the three ways that you can maintain state across Web pages using Visual Basic Scripting Edition.

Following are the three methods:

  • Assign a cookie to an alternate HREF.
  • Use a cookie and change the contents of the page.
  • Use frames and store a value in the top level frame.

MORE INFORMATION

The three methods are described in more detail below. To view an example that demonstrates the three methods, create the HTML files that are described in each section. You can use Notepad or any other text editor to create the files.

Method 1 - Assigning a Cookie to an Alternate HREF

To use method 1, you need to read your files from an HTTP server.

  ******** Begin Page1-1.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub SetCookie
        document.cookie = "MyVar='101'; path='page1-2.htm'"
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 1 - Method 1</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Set Cookie" onClick="SetCookie">
      <A HREF="page1-2.htm">Go to Page 2</A>
    </BODY>
  </HTML>
  ******** End Page1-1.htm **********

  ******** Begin Page1-2.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub GetCookie
        MsgBox document.cookie
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 2 - Method 1</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Get Cookie" onClick="GetCookie">
    </BODY>
  </HTML>
  ******** End Page1-2.htm   **********

Page1-1.htm:
  • The onClick event of the button calls the SetCookie subroutine that is defined at the top of the page.
  • The subroutine sets the value of the cookie and the PATH of the cookie. The PATH is the name of the page that the value is set for.
  • The anchor "Go to Page 2" navigates to page 2 using standard HTML syntax.

Page1-2.htm:
  • The onClick event of the button calls the GetCookie subroutine that is defined at the top of the page.
  • The subroutine reads the value of the cookie to demonstrate that the variable has been set.

Method 2 - Using a Cookie and Changing the Contents of the Page

To use method 2 you need to read your files from an HTTP server.

  ******** Begin Page2-1.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub SetCookie
        document.cookie = "MyVar=101"
      End Sub

      Sub GotoNextPage
        location.href = "page2-2.htm"
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 1 - Method 2</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Set Cookie" onClick="SetCookie">
      <A HREF="" onClick="GotoNextPage">Go to Page 2</A>
    </BODY>
  </HTML>
  ******** End Page2-1.htm **********

  ******** Begin Page2-2.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub GetCookie
        MsgBox document.cookie
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 2 - Method 2</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Get Cookie" onClick="GetCookie">
    </BODY>
  </HTML>
  ******** End Page2-2.htm **********

Page2-1.htm:
  • The onClick event of the button calls the SetCookie subroutine that is defined at the top of the file.
  • The subroutine sets the value of the cookie for the current page.
  • The anchor "Go to Page 2" calls the GotoNextPage subroutine that is defined at the top of the page.
  • The GotoNextPage subroutine navigates to Page 2 by setting the HREF property of the location object. This changes what the current page is pointing to without resetting the cookie.

Page2-2.htm:
  • The onClick event of the button calls the GetCookie subroutine that is defined at the top of the page.
  • The subroutine reads the value of the cookie to demonstrate that the variable has been set.

Method 3 - Using Frames and Storing a Value in the Top Level Frame

  ******** Begin Page3-1.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Dim MyVar
    </SCRIPT>

    <FRAMESET COLS="50%,50%">
      <FRAME SRC="page3-2.htm">
      <FRAME SRC="page3-3.htm">
    </FRAMESET>
  </HTML>
  ******** End Page3-1.htm **********

  ******** Begin Page3-2.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub SetVariable
        top.MyVar = 101
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 1 - Method 3</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Set Variable" onClick="SetVariable">
    </BODY>
  </HTML>
  ******** End PAGE3-2.HTM **********

  ******** Begin Page3-3.htm **********
  <HTML>
    <SCRIPT LANGUAGE="VBSCRIPT">
      Sub GetVariable
        MsgBox top.MyVar
      End Sub
    </SCRIPT>
    <BODY>
      <H2>Page 2 - Method 3</H2><HR>
      <INPUT TYPE=BUTTON VALUE="Get Variable" onClick="GetVariable">
    </BODY>
  </HTML>
  ******** End Page3-3.htm **********

Page3-1.htm:
  • A variable is defined. Since the variable is defined outside of any subroutines, it is accessible to all child frames.

Page3-2.htm:
  • The onClick event of the button calls the SetVariable subroutine that is defined at the top of the file.
  • The subroutine sets the value of the variable in the top-most frame. It does this using the variable TOP, which refers to the top-level frame.

Page3-3.htm:
  • The onClick event of the button calls the GetVariable subroutine that is defined at the top of the page.
  • The subroutine reads the value of the variable in the top-most frame.

REFERENCES

For additional information on cookies and frames visit the Microsoft Site Builder Workshop at http://www.microsoft.com/workshop.


KBCategory: kbprg kbhowto
KBSubcategory: PrgSyntaxVBScript kbdsi
Additional reference words: 1.00 1.10 2.00 kbDSI



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: January 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.