How to Pass a VBScript Variable to an HTML Attribute

How to Pass a VBScript Variable to an HTML Attribute
Page content

Introduction to VBScript

VBScript, short for Visual Basic Scripting Edition, is a Microsoft-created scripting language. Much like its counterpart in Java, JavaScript, it is used extensively to add dynamic functionality to an HTML webpage. However, although this may be its primary use in web browsers, VBScript is not limited to just dynamic content.

The language was designed to allow programmers to reference elements of the supporting environment in their code. For example, when it is used in Internet Explorer, it can be used to call upon elements of the browser which are then manipulated in the code.

Attributes in HTML

HTML is completely composed of various tags, intended to instruct the browser to mark-up text in a certain way. Each of these tags can have a certain set of user-defined characteristics, some of which are compulsory. These characteristics are set as values to variables, which are known as attributes.

Each tag has a set of attributes. While some attributes are generic and can be used with many tags, there are some that are specific to certain tags only. They are placed within the opening tag, after the tag keyword and before the closing bracket.

It is always best to enclose all attribute values in quotes to avoid unpredictable interpretations by various browsers.

Passing VBScript Variables to HTML Attributes

There may be scenarios where the value stored in a VBScript variable has to be passed as a value to an HTML tag attribute. For example, if a user wanted to set the color of webpage using a form, the user input could be accepted through a textbox, stored in a VBScript variable, and then would have to be assigned to the appropriate HTML attribute for the process to work.

However, as HTML and VBScript are not compatible with each in terms of code, the developer needs to find another way to pass the contents of the variable to the attribute in question.

Webpages are always rendered in HTML, even when there is dynamic scripting in the code. The code is converted into HTML as well, and then the webpage is displayed. Therefore, to pass the contents of a variable to an HTML attribute, the developer needs to first extract the contents, and then treat it as text when assigning it.

An illustrative example:

Dim tablecellspace

tablecellspace = InputBox(“Cellspadding for Table: “)

document.write("<table cellspadding =” & cellspadding & " border = 1>”)

The Dim statement declares the variable ‘tablecellspace’ for use. The next statement gets a value from the user, through the use of an input box, and stores in the variable.

The document.write statement indicates that there is output to the webpage. The contents between the parentheses indicate the content that is to be printed on the webpage. All string values are enclosed in quotes, however, even though the variable is treated like text, it is inserted into the function without quotes and hence the contents of the variable will be displayed. If the variable was put into quotes, the name of the variable would become a string.