VBScript Placement : In HTML



Placing VBScript Code :

VBScript can be included in HTML file anywhere but we include VBScript ideally only in following four positions –

  1. Inside <head>…</head> section.
  2. Inside <body>…</body> section.
  3. Inside <body>…</body> and <head>…</head> sections.
  4. Inside external file

Position #1 : Inside Head Section

Generally following position of VBScript placement is preferred when we need to add code for some kind of event such as click event.

<html>
<head>

<script type="text/Vbscript">
<!--
Function message() 
   Msgbox("Hello VBScript")
End Function
//-->
</script>

</head>
<body>
<input type="button" onclick="message()" value="message" />
</body>
</html>

Output :

VBScript output


Position #2 : Inside Body Section

Whenever we need to print some message on page load then we prefer body section to place VBScript code. In the below example VBScript is used to generate content.

<html>
<head>
</head>

<body>

<script type="text/vbscript">
<!--
    document.write("Hello World")
//-->
</script>

</body>
</html>

Output :

Hello World

Position #3 : Inside External File

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.

Consider below external file called script.vbs –

Function printMessage() 
   Msgbox "Hello World"
End Function

Now we need to include the above file in our HTML code then we can write it as –

<html>
<head>
<script type="text/vbscript" src="script.vbs" ></script>
</head>
<body>
.......
</body>
</html>

Explanation :

  1. We use script tag to include external VBScript file in our HTML file.
  2. We use script tag if we need to include same VBScript code in multiple HTML files.
  3. We use script tag so that we can make difference between the VBScript code and HTML code.