VBScript Placement in HTML File
There is a flexibility given to include VBScript code anywhere in an HTML document. But the most preferred way to include VBScript in your HTML file is as follows −
- Script in <head>…</head> section.
- Script in <body>…</body> section.
- Script in <body>…</body> and <head>…</head> sections.
- Script in an external file and then include in <head>…</head> section.
In the following section, we will see how we can put VBScript in different ways −
VBScript in <head>…</head> section
If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head as follows −
<html> <head> <script type = "text/Vbscript"> <!-- Function sayHello() Msgbox("Hello World") End Function //--> </script> </head> <body> <input type = "button" onclick = "sayHello()" value = "Say Hello" /> </body> </html>
It will produce the following result − A button with the name SayHello. Upon clicking on the Button, the message box is displayed to the user with the message “Hello World”.
Hello world say hello
VBScript in <body>…</body> section
If you need a script to run as the page loads so that the script generates content in the page, the script goes in the <body> portion of the document. In this case, you would not have any function defined using VBScript −
<html> <head> </head> <body> <script type = "text/vbscript"> <!-- document.write("Hello World") //--> </script> <p>This is web page body </p> </body> </html>
This will produce the following result −
Hello World This is web page body
VBScript in <body> and <head> sections
You can put your VBScript code in <head> and <body> section altogether as follows −
<html> <head> <script type = "text/vbscript"> <!-- Function sayHello() msgbox("Hello World") End Function //--> </script> </head> <body> <script type = "text/vbscript"> <!-- document.write("Hello World") //--> </script> <input type = "button" onclick = "sayHello()" value = "Say Hello" /> </body> </html>
It will produce the following result − Hello World message with a ‘Say Hello’ button. Upon Clicking on the button a message box with a message “Hello World” is displayed to the user.
Hello World
VBScript in External File
As you begin to work more extensively with VBScript, you will likely find that there are cases, where you are reusing identical VBScript code on multiple pages of a site. You are not restricted to be maintaining identical code in multiple HTML files.
The script tag provides a mechanism to allow you to store VBScript in an external file and then include it into your HTML files. Here is an example to show how you can include an external VBScript file in your HTML code using script tag and its src attribute −
<html> <head> <script type = "text/vbscript" src = "filename.vbs" ></script> </head> <body> ....... </body> </html>
To use VBScript from an external file source, you need to write your all VBScript source code in a simple text file with extension “.vbs” and then include that file as shown above. For example, you can keep the following content in filename.vbs file and then you can use sayHello function in your HTML file after including filename.vbs file.
Function sayHello() Msgbox "Hello World" End Function
VBScript Placement in QTP
VBScript is placed in QTP (Quick Test Professional) tool but it is NOT enclosed within HTML Tags. The Script File is saved with the extension .vbs and it is executed by Quick Test Professional execution engine.