Using Web Scripting

Web pages commonly use scripting languages to define actions and objects accessible from within a browser. The most common languages in use are JavaScript (Microsoft's version is called JScript) and VBScript (Visual Basic Script). To add JavaScript or VBScript code to a new Web page, do the following:

  1. Choose Tools, Macro, Microsoft Script Editor from the menu. Or use the keyboard shortcut, Alt+Shift+F11. The Microsoft Script Editor opens up as shown in Figure 24.21, although with a different view from that in typical HTML source viewing.

    Figure 24.21. Adding script code to a Web page using the Microsoft Script Editor.

    graphics/24fig21.jpg

    If the Script Editor is not already installed, you will be prompted to install it; follow the prompts.

    NOTE

    The Script Editor view for typical HTML source (refer to Figure 24.8) does not open the Toolbox, Properties window, or Project Explorer window used to create or edit scripting code as shown in Figure 24.21.

  2. To choose the default language (either VBScript or JavaScript) in which to write a script, choose View, Property Pages from the menu (Shift+F4 from the keyboard). This affects scripts for which you do not explicitly specify a language. As shown in Figure 24.22, you can choose between VBScript and JavaScript (ECMAScript) for the Client.

    Figure 24.22. Choosing the scripting language to use in a Web page.

    graphics/24fig22.gif

    CAUTION

    JavaScript is the more universally supported language. VBScript works only in Internet Explorer, not in Netscape Navigator.

    NOTE

    Client-side scripts execute within the browser after the Web page has loaded; they are self-contained.

    The scripting language can also be changed in the Properties window of the Script Editor under defaultClientScript.

  3. After you've selected a scripting language, scroll to the insertion location in the Web page. Right-click and choose Insert Script Block, Client. This command is also available from the menu under Edit, Insert Script Block, Client.

    A typical blank scripting container for JavaScript would appear as follows:

    
    
    <script language=javascript>
    <!--
    
    //-->
    </script>
    

    NOTE

    Those symbols <!-- and //--> are used to enclose comments in HTML, so the HTML interpreter portion of a Web browser will not read them. This leaves other interpreters such as VBS or JavaScript free to claim them as their own and run them when the page loads.

  4. After the container is created, you can start typing code.

If you prefer to select scripting functions from a list, click the Display an Object Member List button on the Text Editor toolbar (or press Ctrl+J). An object member list appears, as shown in Figure 24.23. Scroll and choose members as needed. After you've selected a member, you can click on the Display a Parameter List button in the Text Editor toolbar to view the proper syntax for a member.

Figure 24.23. Choosing scripting members from a pop-up list.

graphics/24fig23.jpg

The Microsoft Script Editor includes many of the amenities found in sophisticated programming environments, such as debugging and the capability to insert breakpoints. You can completely test and debug your script from within the Script Editor.

NOTE

You may need to install the debugging features in the Script Editor before use. The Debug menu consists of only one item, Install Web Debugging, if debugging features aren't installed.


When the script works to your satisfaction, save the file and exit the Script Editor to return to Word.

CAUTION

Word generates the HTML code. If you resave the Web page from within Word, any manual changes you made from the Script Editor may be overwritten and lost.




    Part I: Word Basics: Get Productive Fast
    Part II: Building Slicker Documents Faster
    Part III: The Visual Word: Making Documents Look Great
    Part IV: Industrial-Strength Document Production Techniques
    Part VI: The Corporate Word