Generating HTML on-the-fly


© Maxim Karetnikov

As far as you've learned some of the important fundamentals of VBScript programming, it's time to see to put these techniques to practical use. One of the coolest features of VBScript is its capability to generate HTML on-the-fly. You can use the programming capabilities of VBScript to decide what should appear on the page and how it should appear. Then the script code can output the HTML for display within the browser window.

You can use three methods to generate HTML using VBScript: within the window_onLoad event (which fires when the page loads), embedded anywhere within the HTML file, and as a new page created on demand within the browser window. methods.

Using the onLoad Event

When a page is loaded into the Internet Explorer, an event named window_onLoad fires. You can write code in your script to handle this event by adding a code section such as one shown below.

<html>
<body>
<script language="VBscript">
sub window_onload
document.writeln "<h3>Hi,</h3>"
document.writeln "<font color='red'>Today is " & Date & "," & Time & "</font>"
end sub
</html>

Any code you place within this procedure is executed when the page is loaded. You also can use the document.writeln method to output any HTML text to the page. After you finish writing the HTML, use the document.close method to close the document's output stream and actually display the HTML generated by the script.

Embedding VBScript in the HTML

In addition to using the onLoad event, you can embed VBScript code anywhere within your page's HTML. This is useful when most of your page is based on static HTML but you want to use VBScript code to display some HTML that changes with varying conditions. A good example of this is displaying the current date on the page. You also can use this technique to embed different graphics and messages, depending on any variable information to which VBScript has access: time of day, type of Web browser the user is running, and even the history list that contains the last few pages the user visited.

Creating a New Page with VBScript

The final method discussed here involves actually creating a new Web page on-the-fly. When this method is used, the current page is cleared and the new page is displayed in its place. Say, you have clicked the Submit button.

<html>
<body>
<p>Enter a title of the page<input type="text" name="txtTitle"></p>
<p>Enter a heading of the page<input type="text" name="txtHead"></p>
<p>Enter a text <input type="text" name="txtText"></p>
<p><input name="cmdSubmit" type="submit" value="Generate your page"></p>

Go To Page: 1 2


The copyright of the article Generating HTML on-the-fly in VB Script is owned by . Permission to republish Generating HTML on-the-fly in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo