Share via


Creating Scripts in the HTML Editor

The HTML editor includes features that help you create scripts. You can create event handlers for elements on the page, which are scripts that run in response to actions such as when a user chooses a button, when a document first loads, or another event. You can also use editor features to create standalone script blocks to contain any script, not only event handlers.

Tip   If you insert script into the body portion of a document, choose Show Details in the Design toolbar. This displays a glyph for the script when you are in Design view. Without the glyph, the script does not appear in Design view, and you might delete it accidentally.

To create event-handling scripts, you use the Script Outline window. The Script Outline window shows you all scriptable elements in your page, and for each element, the events you can write handlers for. The Script Outline window divides the elements and scripts into those that will run on the client and those that will run on the server.

In the editor, you write scripts in Source view. However, if you are working in Design view, you can start scripting for an element you are working with.

To start scripting from Design view

  • Double-click the element you want to write an event handler for, or right-click it, and then choose Edit Script.

    The editor switches to Source view. It also displays the Script Outline window and places the focus on the default handler for that element. If the element you clicked does not have an ID attribute, the HTML editor adds one.

With the Script Outline window displayed, you can create a skeleton event handler for any element on the page.

To create an event handler

  1. In the Script Outline window, expand the node of the element you want to write a handler for.

  2. Double-click the event you want to script.

When you double click the event name, the editor performs the following actions:

  • Creates or moves to one of the following script blocks at the top of the document, depending on where the script will run and what language it will be in:

    • clientEventHandlersJS

    • clientEventHandlersVBS

    • serverEventHandlersJS

    • serverEventHandlersVBS

  • Inserts a new, blank event-handling procedure for the element and event you specified.

  • If the script will be in Javascript, adds an event attribute (for example, onclick=) to the element.

  • Positions the insertion point at the second line of the new script, ready for you to enter commands.

The language used for an event handler skeleton depends on the default language for client or server script, as set in the Property Pages dialog box for the current document. For VBScript functions, the format is:

Sub elementID_event

End Sub

For JavaScript functions, the format is:

function elementID_event(){

}

When creating JavaScript event handlers, the editor also adds the following attributes to the HTML element itself:

event="return elementID_event()"

In addition to creating event handlers, you can create standalone script blocks. This is useful if you want to create procedures (subroutines or functions) called by other scripts, or if you want to create global script that runs as soon as the page is processed by the browser.

To create a standalone script block

  1. Switch to Source view.

  2. Move to the location in the document where you want the script to appear.

  3. From the HTML menu, choose Script Block, and then choose Client or Server.

    The HTML editor creates a <SCRIPT> block. If you choose Server, the script tag contains the attribute RUNAT=SERVER. The script block's LANGUAGE attribute is set to the default language for the client or server.