An HTML file can contain scripts that execute in a Web browser, assuming that no options have been set to prevent the scripts from running. Microsoft Office 2000 documents can contain user-specified JavaScript and VBScript code. The script is stored as a shape object in the native binary file format of the Office application. However, when the code is saved to a Web page, only the script block is saved, not the shape. Office HTML files can also contain JavaScript code automatically generated by the Office application and used to automate frame loading and navigation controls.
Script code is contained within an HTML Script element, and there can be one or more Script elements in an HTML file. The code is organized in functions and subroutines within the element. These procedures can be executed by an explicit call or triggered when a specific event occurs, such as when a button is clicked. In VBScript, code can be specified in a Script element but outside of function and subroutine procedures. The code executes as soon as the page starts to open in the Web browser. Because the code executes before the page has finished loading, the Script element can contain initialization code that is executed before the Web browser finishes loading the page. The initialization code is similar to the type of code that goes in a Visual Basic Form_Load event handler to prepare a form for use before the form is displayed. An example of initialization code is the JavaScript in supporting HTML files that redirects the Web browser to open the main page. However, because the Web page may not be completely loaded, initialization code that relies on controls and objects on the Web page should be avoided.
Script can be specified in the body or head of an HTML document. Initialization code is typically stored in the head since the beginning of the document is loaded first.