VKS-LEARNING HUB

Home » Class-XI » MMWT-XI » VB Script

VB Script

Microsoft’s VBScript (Visual Basic Script) is a scripting language used to create dynamic and interactive web pages. VBScript is a subset of Visual Basic, a more developed scripting language, and is commonly used on the Web as a client side scripting language and server-side processing in ASPs (Active Server Pages). The interpreted script language VBScript is designed for Web Browser interpretation. VBScript is similar to scripting languages, including; Netscape’s JavaScript, Sun Microsystem’s Tcl, IBM’s Rexx and the UNIX-derived Perl. These scripting languages have been designed to be used as an extension for html language. A Web Browser receives the scripts for websites through web page documents that are then parsed and processed.

VBScript, like JavaScript (Jscript) is an ActiveX-enabled scripting language that connects to scripting hosts such as Internet Explorer and performs functions locally using the Windows Script Host (WSH). As a general rule, scripting languages are coded faster and simpler than in the compiled languages of C and C++. VBScript is structured and used with smaller programs with limited capability. Programmers, developers and IT professionals learning VBScript should also be familiar with ASP (Active Server Pages) and possess SQL Server commercial experience. Tutorials on VBScript include controlling script routines, working with objects, variables, forms and general VBScript information.

The Script and the Document

The SCRIPT Tag

You can use the SCRIPT element to add VBScript code to an HTML page.

The <SCRIPT> Tag
VBScript code is written within paired <SCRIPT> tags.
To set the instructions of a script from the HTML tags, the section that has the script must start with the <SCRIPT> tag and end with the </SCRIPT> tag, as follows:

You can write the tag in all uppercase, all lowercase, or a mix

To use a VBScript in a page, start the section with <Script language=”VBScript”> and end it with the closing tag. Therefore the scripting section can be delimited with:

<Script Language=”VBScript>

     

 </Script>

Like HTML, VBScript is not case sensitive. This means that script, SCRIPTscript, and Script are the same. Therefore, you can also include a script in the following:

<SCRIPT LANGUAGE=”VBScript”>

     

</SCRIPT>

The section between the opening <Script> tag and the closing </Script> tag is called the body of the script. Everything that is part of the body of a script belongs to the script.

The Document Object

VBScript uses an object called Document. This object manages many of the instructions that VBScript can handle for HTML. One of the functions of that object is to display a string on the screen. A string is text that the browser is asked to use “as is”. The function used is calledWrite. The syntax of the Write function of the Document object is:

Document.Write(String)

The String to display can be provided in double-quotes or as a variable as we will learn soon. Here is an example of displaying somebody’s name using the Document.Write() function:

<SCRIPT LANGUAGE=”VBScript”>

     Document.Write(“Welcome to VB SCRIPT”)

</SCRIPT>

To display various lines of text, you can use as many Document.Write() lines as you need.

A file can have many different script sections and you can include as many lines of code as necessary in a file. To have different script sections, start each with the <SCRIPT> tag and close it with the closing tag. Here is an example:

<SCRIPT LANGUAGE=”VBScript”>

Document.Write(“Book Title: “)

Document.Write(“VBScript Programming”)

</SCRIPT>

<SCRIPT LANGUAGE=”VBScript”>

Document.Write(“Welcome To My Site”)

Document.Write(“<br>”)

Document.Write(“Blog for Multimedia & Web Technology”)

Document.Write(“<br>”)

</SCRIPT>

Link

INPUTBOX

The InputBox function displays a dialog box, where the user can write some input and/or click on a button. If the user clicks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks on the Cancel button, the function will return an empty string (“”).

 

 

Example 1

<script type=”text/vbscript”>
fname=InputBox(“Enter your name”)
document.write(fname)
</script>

Example 2

A prompt box with a title:

<script type=”text/vbscript”>Function myFunction()
fname=InputBox(“Enter your name”,”Userinput”)
End Function</script>
 

Example 3

A prompt box with a deafult text in the inputbox:

<script type=”text/vbscript”>Function myFunction()
fname=InputBox(“Enter your name”,,”Donald Duck”)
End Function</script>
 

Example 4

A prompt box wich is positioned 700 twips* from the left edge of your screen.

<script type=”text/vbscript”>Function myFunction()
fname=InputBox(“Enter your name”,,,700)
End Function</script>

 

MSGBOX

The MsgBox function displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked.

Syntax

MsgBox(prompt[,buttons][,title][,helpfile,context])

The MsgBox function can return one of the following values:

  • 1 = vbOK – OK was clicked
  • 2 = vbCancel – Cancel was clicked
  • 3 = vbAbort – Abort was clicked
  • 4 = vbRetry – Retry was clicked
  • 5 = vbIgnore – Ignore was clicked
  • 6 = vbYes – Yes was clicked
  • 7 = vbNo – No was clicked

Example 1

<script type=”text/vbscript”>MsgBox(“Hello world”)</script>

Example 2

A messagebox with a line feed:

<script type=”text/vbscript”>MsgBox(“Hello” & chr(13) & “world”)</script>

Example 3

Different buttonsets and different icons. Returns the value of the clicked button:

<script type=”text/vbscript”>x=MsgBox(“Hello world”,n)
document.getElementById(“myDiv”).innerHTML=”You clicked: ” & x</script>

Example 4

A messagebox with a title:

<script type=”text/vbscript”>x=MsgBox(“Are you a programmer”,4,”Please answer”)</script>

Leave a comment