VBscripts can be placed in the body and in the head section of an HTML document.
Where to Put the VBscript
VBscripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such as when a user clicks a button. When this is the case we put the script inside a function or a sub procedure, you will learn about procedures in a later chapter.
scripts in
Put your functions and sub procedures in the head section, this way they are all in one place, and they do not interfere with page content.
Example (IE Only)
function myFunction()
alert("Hello World!")
end function
Try it yourself »
scripts in
If you don't want your script to be placed inside a function, and especially if your script should write page content, it should be placed in the body section.
Example (IE Only)
document.write("This message is written by VBscript")
Try it yourself »
scripts in and
You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section.
Example (IE Only)
function myFunction()
alert("Hello World!")
end function
document.write("This message is written by VBscript")
Try it yourself »
Using an External VBscript
If you want to run the same VBscript on several pages, without having to write the same script on every page, you can write a VBscript in an external file.
Save the external VBscript file with a .vbs file extension.
Note: The external script cannot contain the tag!
To use the external script, point to the .vbs file in the "src" attribute of the tag:
Example
Try it yourself »
Note: Remember to place the script exactly where you normally would write the script!
Where to Put the VBscript
VBscripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such as when a user clicks a button. When this is the case we put the script inside a function or a sub procedure, you will learn about procedures in a later chapter.
scripts in
Put your functions and sub procedures in the head section, this way they are all in one place, and they do not interfere with page content.
Example (IE Only)
function myFunction()
alert("Hello World!")
end function
Try it yourself »
scripts in
If you don't want your script to be placed inside a function, and especially if your script should write page content, it should be placed in the body section.
Example (IE Only)
document.write("This message is written by VBscript")
Try it yourself »
scripts in and
You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section.
Example (IE Only)
function myFunction()
alert("Hello World!")
end function
document.write("This message is written by VBscript")
Try it yourself »
Using an External VBscript
If you want to run the same VBscript on several pages, without having to write the same script on every page, you can write a VBscript in an external file.
Save the external VBscript file with a .vbs file extension.
Note: The external script cannot contain the
To use the external script, point to the .vbs file in the "src" attribute of the
Example
Try it yourself »
Note: Remember to place the script exactly where you normally would write the script!