Click to return to the Reusing Browser Technology home page    
IHTMLBodyElement::get_now...     IHTMLBodyElement::get_onl...     IHTMLBodyElement Interfac...    
Web Workshop  |  Reusing Browser Technology

IHTMLBodyElement::get_onbeforeunload Method


Retrieves a pointer to the event handler function associated with the onbeforeunload event on the BODY element.

Syntax

HRESULT get_onbeforeunload(
    VARIANT *p
);

Parameters

p
Address of a VARIANT structure to store the script.

Return Value

Returns S_OK if successful, or an error code otherwise.

Remarks

The handler for this event should return NULL if doing the unload is safe, or a text string to prompt the user to decide to cancel or continue with the unload operation. Only the user's response to the warning can stop the new navigation causing the unload; the event handler itself cannot unilaterally block the operation. If the handler returns a non-NULL string, a modal dialog box appears that displays the explanatory string. The dialog box shows three buttons with the message for user action:

In the case of frames, the full sequence of onbeforeunload events is fired on each frame in the same order, depth-first, that onunload events are about to be fired. A dialog box is presented for each event handler that returns a non-NULL string. The first Cancel response stops the potential navigation, in which case no unload occurs. If there is no Cancel response, the unload proceeds and onunload events fire.

Use the following code fragment to obtain an event handler.
pBody->get_onEvent( &_variant_t(pDisp) ); 
// pDisp a pointer to an IDispatch interface
// pBody a pointer to an element of interest


IHTMLBodyElement::put_onbeforeunload Method


Sets a pointer to the event handler function associated with the onbeforeunload event on the BODY element.

Syntax

HRESULT put_onbeforeunload(
        VARIANT v
);

Parameters

v
VARIANT structure that contains the script to set.

Return Value

Returns S_OK if successful, or an error code otherwise.

Remarks

The handler for this event should return NULL if doing the unload is safe, or a text string to prompt the user to decide to cancel or continue with the unload operation. Only the user's response to the warning can stop the new navigation causing the unload; the event handler itself cannot unilaterally block the operation. If the handler returns a non-NULL string, a modal dialog box appears that displays the explanatory string. The dialog box shows three buttons with the message for user action:

In the case of frames, the full sequence of onbeforeunload events is fired on each frame in the same order, depth-first, that onunload events are about to be fired. A dialog box is presented for each event handler that returns a non-NULL string. The first Cancel response stops the potential navigation, in which case no unload occurs. If there is no Cancel response, the unload proceeds and onunload events fire.

Use the following code fragment to set an event handler. This method will only add a single event handler. To add multiple event handlers, see the IHTMLDocument3::attachEvent, IHTMLElement2::attachEvent, and IHTMLWindow3::attachEvent methods.

pBody->put_onEvent( &_variant_t(pDisp) ); 
// pDisp a pointer to an IDispatch interface
// pBody a pointer to an element of interest


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.