Using Events to Control Actions

See Also

There are two types of events in Microsoft FrontPage, events that are driven off the Application object model and events that are driven off the Page object model. The events for the Application object model can be used to control under what conditions a web is published, whether you want to save a page whenever the OnPageClose event is fired, or whether you want to set styles, fonts, or backgrounds whenever a new page is created. For more information on the Application object model events, click one of the events in the following list.

OnAfterPageSave OnPageNew
OnBeforePageSave OnPageOpen
OnAfterWebPublish OnWebClose
OnBeforeWebPublish OnWebNew
OnPageClose OnWebOpen

In the Microsoft Internet Explorer DHTML object model, event handlers are created using one of the following methods:

You can declare an event handler function by creating a procedure or function inside of a script tag and then define an "On*" attribute that calls that procedure for the element you that you want to generate the events. For example, to catch a mouse click on an anchor you could use the HTML code shown in the following example.

<html>
<head>
<script language="VBScript">
sub test
msgbox "You clicked"
End Sub
</script>
</head>
<body>
<a href="CatchMouse.htm" onclick="CatchMe"> This is an anchor, click here</a>
</body>
</html>

Another way to declare event handling code is to create a script tag with a FOR=attribute, as shown in the following example.

<SCRIPT FOR=myanchor EVENT=onclick LANGUAGE="VBScript">
msgbox "You Clicked"
</SCRIPT>
<a id="myanchor" href="CatchMouse.htm" onclick="CatchMe"> this is an anchor, click on it </a>

However, in the FrontPage Page object model, you're programming with events and objects that are compatible with Internet Explorer, but for use at design-time. To program events for run-time, you can use the standard Microsoft Visual Basic 5.0 keywords to access the Page object model events just as you would to access the Web object model events. This method combines the two techniques described previously. The following example catches the OnClick event for a hyperlink in FrontPage.

In the Visual Basic Editor, insert a class module and name it CatchOnClick. Add the following code to the class class module.

Dim WithEvents eAnchor As FPHTMLAnchorElement
Dim WithEvents eDoc As FPHTMLDocument
Dim e As IHTMLEventObj

Private Sub Class_Initialize()
Set eDoc = ActiveDocument
Set eAnchor = eDoc.links(0)
End Sub

Private Function eAnchor_onclick() As Boolean
Set e = eAnchor.Document.parentWindow.event
If (MsgBox("OnClick Event for " & e.srcElement.tagName & _
    " would you like to cancel the event bubbling?", _
    vbYesNo) = vbYes) Then
    e.cancelBubble = True
    e.returnValue = False
Else
    e.cancelBubble = False
    e.returnValue = True
End If
End Function

Private Function eDoc_onclick() As Boolean
MsgBox "OnClick event for the Document object"
End Function

Next add a standard module and add the following code.

Public e As CatchOnClick
Sub GetClick()
Set e = New CatchOnClick
End Sub

Note   To run the example, perform the following steps:

A prompt displays stating that the OnClick event fired. The prompt also queries the user—should the program pass the event on up the event chain? If Yes is chosen, the OnClick event is passed up to the document object to be handled.

To control which document object the event is passed to, you must set both the cancelBubble and returnValue events. The cancleBubble event works to cancel the event from going any farther up the event chain. Set the cancelBubble property for the IHTMLEventObj to True when you don't want the OnClick event to be passed up to the next level of OnClick events, otherwise, set the cancelBubble property to False. For example, if you have an image that has an OnClick event placed on a document, which also has an OnClick event, you would set the cancelBubble property for the IHTMLEventObj object to True for the image, if you don't want the OnClick event to be passed on up to the document OnClick event.

The returnValue propertyis used to control the default action taken by FrontPage when an event fires. Using the previous example of an image placed on a document, if the returnValue property for the IHTMLEventObject for the image is set to False in the OnClick event, then the right-click context menu would be disabled (because the right-click context menu is the default action for the OnClick event).