The Methods of the Document Object VB Help

The Document object supports a few methods as well, which let you manipulate its contents. The Document object’s methods manipulate the contents of the current document.

Open Thismethod opens the document for output The current document  cleared, and new strings can be placed on the document with the Write and Write methods.

Write string This method writes the string variable to the document. The argument is inserted in.the current document at the current position, but it doesn’t appear until the document is closed with the.Close ,method.

WrlteLn string Thismethod writes the string variable into the current document with a newline character appended to the end. The newline character is ignored by , the browser anyway, so the WriteLn string method is practically the slme as the Write string method.

Close Thismethod closes the document and causes all the information written to it with the Write and WriteLn methods to be displayed, as if it were placed in an HI’ML document that is loaded in the browser’s window. Clear This method clears the contents of the document.

Using the Document Object’s Methods

In effect, these methods allow the programmer (or Web author) to create an HI’ML document from within the sCript,html ‘Pagel.htm example of the next section demonstrates, The DocUment object’s methods are usually called in the following order:

The string variable, or literal, could be anything that normally appears in an HrML document (text, HTML tags, hyperlinks, and so on). Because the Write method’s argwne8t can contain HI’ML tags, you have the flexibility to create Web pages on the fly. The following statements ttisplay a levell header; centered on the page

you take ,tht arguments of the Write methods and strip the quotes, you’ll get the HTML document that would produce the same page.

The most common USE’ of these methods is to create documents on thelly. The Write method is extremely flexible, and we are’ going to look at a couple of examples.

VB6 at Work: The Navigate Page

The Navigate.atm document, shown in Figure 21.9, contains a floating frame and two buttons. The first button displays a user-specified URL in the frame by calling the Wmdow object’s Navigate method. The second button also displays the user-specified URL in the frame, only this one uses the hRef property of the Location object.

The floating frame was inserted with the following statement:

The Command buttons were placed on the page with two <INPUT> tags, and their names are Button1 and Butt0n2. The two buttons react to the onClick event with the fo!1owinghandlers. The first button’s code sets the frame’s destination to a userspecified URL and is as follows:

The .URLs tnust be complete, including their protocol ~part. To navigate to Microsoft’s home page, enter the URL http://home.mi crosoft .com,and not just home.microsoft.cOm.

VB6 at Work: The Calendar.htm Page

The page shown in Figure 21.10 was created entirely from within a script. The calendar is generated by VBScript code on the client’s side and displays the days of tbe current month. Byspecifying a different date you can create any month’s calendar. The actual calendar doesn’t react to mouse clicks, but you can easily turn the dates into hyperlinks that point to documents with information specific to each date. To understand the code of this application, you need ‘a basic knowledge of building tables with HTML tags. If you are familiar with these tags, you’ll find the Calendar page’s script straightforward.

Next, the program displays the days of the first week with a For Next loop. The first week of the month is frequently incomplete, and the first few cells in the table are likely to be blank. This loop goes through the seven days in the week Until it hits the first day in the month.

After the first day in the month is found, the program creates cells in which it ( places the value of the variable ne~tday, which is increased with every iteration goes from 1 to 31). The following string produces a cell with a number:

This is HTML code, and any references to variables are replaced with the actualy of the nextday variable. For example, if the value of the nextday variable Is 24, the following line is actually written to the document

After the first week of the calendar is displayed, the program continues with the following weeks. These weeks are complete, except for the last one, of cour. A While …Wend loop handles the remaining days of the month. At each iteration, the ‘nextdry variable is increased by one day, and the loop continues to the end of the month. You can easily turn each day of the month into a hyperlink that points to a file on the server. If you maintain a separate document on the server for each day 0 the month, you can modify the application so that each day is a hyperlink to to date’s file. Instead of writing the number of the day to the output, you can inse the appropriate <A> tags to turn the day number into a hyperlink. For example if the current value of the nextday variable is 24, the following VBScript line

The names of the files specified in the hyperlinks must also exist on the server 0 in the same folder as the document that opened them.

Posted on November 6, 2015 in Visual Basic and the Web

Share the Story

Back to Top