VBScript
  1. VBScript intro
  2. VBScript basics
  3. VBScript variables
  4. VBScript procedures
  5. VBScript popup boxes
  6. VBScript conditions
  7. VBScript loops
  8. VBScript arrays
  9. VBScript strings
  10. VBScript date & time
  11. VBScript for IE 11
  12. VBScript summary

VBscript basics

VBScript code can be declared in various places and can be used to print text as well as contain HTML tags.

This tutorial focuses on:

Declaring a script

A VBScript script is declared using HTML's <script> tag. This tag denotes that there will be a script on a webpage, and the scripting language that will be used in the script is denoted by this tags type attribute. When working with VBScript, it should be set to "text/vbscript"

Example:
<script type="text/vbscript"> script content goes here </script>

A script can be placed in the head section or the body section of a webpage, but a it will be executed differently depending on where it is placed.

A script placed in the head section of a webpage

A script placed in the head section of a webpage will be executed when it is called, or when certain events are triggered such as the clicking of a button or when a form is submitted.

Example:
<html> <head> <script type="text/vbscript"> document.write("This is a script") </script> <title>VBScript script in the head section</title> </head> <body> </body> </html>

A script placed in the body section of a webpage

A script placed in the body section of a webpage will be executed when the page loads. A script placed in the body section of a webpage generates the content of the page.

Example:
<html> <head> <title>VBScript script in the body section</title> </head> <body> <script type="text/vbscript"> document.write("This is a script") </script> </body> </html>

You can have as many scripts on a webpage as you want, this includes both scripts in the head section and the body section of a webpage.

Script in both head and body:
<html> <head> <script type="text/vbscript"> document.write("This is a script") </script> <title>VBScript script in the head and body sections</title> </head> <body> <script type="text/vbscript"> document.write("This is a script") </script> </body> </html>

Printing text on a webpage

Printing text on a webpage is accomplished with the document.write() command.

Syntax:
document.write("textToPrint")
Example:
<html> <head> <title>Printing text</title> </head> <body> <script type="text/vbscript"> document.write("Here is some text") </script> </body> </html>
Output:
Here is some text

Including HTML tags in a script

HTML tags can be included in a script using the document.write() command mentioned above. Any tags included in a script through this command will be interpreted by the web browser as regular HTML.

Example:
<html> <head> <title>HTML tags in scripts</title> </head> <body> <script type="text/vbscript"> document.write("<b><i>Here is some bold italic text</i></b>") </script> </body> </html>
Output:
Here is some bold italic text

Including comments in a script

Comments in VBScript are declared so that code would be easier to understand and navigate. Comments are not seen on a webpage, but only within the source code of a webpage. Comments can be placed anywhere within VBScript source code. In VBScript you can have only single line comments, no multi-line comments.

Single line comments in VBScript are declared with the apostrophe ( ' ) symbol.

Example:
<html> <head> <title>Comments</title> </head> <body> <script type="text/vbscript"> 'this is a single line comment 'this is another single line comment document.write("Here is some text") </script> </body> </html>

Dealing with browsers that do not support scripts

There are older browsers still in use that do not recognize the <script> tag and consequently will not be able to execute scripts. In such a case, the content inside the <script> tag will be displayed on the page as regular text. To prevent this from happening, the content of a script can be placed within HTML comment tags. In such a case, older browsers that do not recognize the <script> tag will ignore the script and the content inside the <script> tag will not be displayed on the page. Browsers that can execute scripts will ignore the comments and execute the script anyway.

Example:
<html> <head> <title>Dealing with browsers that do not support scripts</title> </head> <body> <script type="text/vbscript"> <!-- document.write("Here is some text") --> </script> </body> </html>
Output:
Here is some text

Handling browsers that do not support VBScript

There may be a situation where a user is using a browser that supports scripting, but not VBScript in particular. In such a situation, you can use HTML's <noscript> tag.

Example:
<html> <head> <title></title> </head> <body> <script type="text/vbscript"> <!-- document.write("Your web browser supports VBScript!") --> </script> <nsocript> Your web browser does not support VBScript! </noscript> </body> </html>

In this example, if VBScript is supported then the script will execute and the message "Your web browser supports VBScript!" will be printed onto the page. If VBScript is not supported then the script will not be executed and the message found in the <noscript> tag which states "Your web browser does not support VBScript!" will be printed onto the page.

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information