BUG: OnBeforeUnload() Does Not Fire When Hosting WebBrowser Control (253201)
The information in this article applies to:
- Microsoft Internet Explorer (Programming) 4.0
- Microsoft Internet Explorer (Programming) 4.01
- Microsoft Internet Explorer (Programming) 4.01 SP1
- Microsoft Internet Explorer (Programming) 4.01 SP2
- Microsoft Internet Explorer (Programming) 5
- Microsoft Internet Explorer (Programming) 5.01
- Microsoft Internet Explorer (Programming) 5.5
This article was previously published under Q253201 SYMPTOMS
When hosting a WebBrowser control in either a Microsoft Visual C++ or Microsoft Visual Basic application, an HTML page's OnBeforeUnload event handler is never called when the application is terminated, even though the OnUnload event handler is called. The handler is called properly when viewing the page in a standalone instance of Internet Explorer.
CAUSE
In Internet Explorer, this event is fired from outside of the WebBrowser control by calling the WebBrowser's IOleCommandTarget's Exec() method with the OLECMDID_ONUNLOAD.
RESOLUTION
To resolve this problem, send the WebBrowser the OLECMDID_ONUNLOAD command in your host application. This can be accomplished by calling the WebBrowser control's ExecWB() method.
To fully emulate Internet Explorer's behavior, you must examine the return value of ExecWB's fourth argument, which tells you whether or not the shutdown attempt failed or succeeded. This is because script authors can use the OnBeforeUnload event to ask the user whether they want to leave the page or not. If the user answers no, the WebBrowser should remain activated. Visual C++
HRESULT hr;
VARIANT vOut;
VariantInit(&vOut);
hr = pWebBrowser2->ExecWB(OLECMDID_ONUNLOAD, OLECMDEXECOPT_DODEFAULT, NULL, &vOut);
if (SUCCEEDED(hr)) {
// Check VT_BOOL return value of vOut to make sure we're really shutting down - IE or the user
// might decide not to honor this request.
if (vOut.bVal == TRUE) {
// Continue shutdown process
} else {
// recover
}
}
Visual Basic
Public Sub Form_Unload(Cancel as Integer)
WebBrowser1.ExecWB OLECMDID_ONUNLOAD, OLECMDEXECOPT_DODEFAULT, 0, ret
If ret = False Then
Cancel = 1
End If
End Sub
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. REFERENCES
See the MSDN Library for more information on the OnBeforeUnload event.
Modification Type: | Major | Last Reviewed: | 5/12/2003 |
---|
Keywords: | kbBug kbnofix kbWebBrowser KB253201 |
---|
|