Microsoft Knowledge Base Email Alertz

KBAlertz.com: 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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]






Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

BUG: OnBeforeUnload() Does Not Fire When Hosting WebBrowser Control

Retired KB ArticleThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Article ID:253201
Last Review:May 12, 2003
Revision:2.0
This article was previously published under Q253201
On This Page

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.

Back to the top

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.

Back to the top

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
				

Back to the top

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

Back to the top

MORE INFORMATION

Steps to Reproduce this Behavior

1.Open Visual Basic and create a new Standard EXE.
2.Select Components from the Project menu and include Microsoft Internet Controls in your project.
3.Place a copy of the WebBrowser control on your application's form.
4.Save the following file onto your hard drive as Test.htm:
<html>

<HEAD>
<SCRIPT>
function unload() {
	alert("Inside unload");
}
function beforeunload() {
	alert("Inside beforeunload");
}
</SCRIPT>
</HEAD>

<body onunload="unload();" onbeforeunload="beforeunload();">
</body>

</html>
					
5.In your form's Load function, add code to navigate to this file:
    WebBrowser1.Navigate "C:\temp\test.htm"
					
6.Run this application from the Visual Basic Integrated Development Environment (IDE). As soon as the page fully loads, close the form. You will see an "Inside unload" message, but will never see an "Inside beforeunload" message.

Back to the top

REFERENCES

See the MSDN Library for more information on the OnBeforeUnload event.

Back to the top


APPLIES TO
•Microsoft Internet Explorer 4.0 128-Bit Edition
•Microsoft Internet Explorer 4.01 Service Pack 2
•Microsoft Internet Explorer 4.01 Service Pack 1
•Microsoft Internet Explorer 5.0
•Microsoft Internet Explorer 5.01
•Microsoft Internet Explorer 5.5

Back to the top

Keywords: 
kbbug kbnofix kbwebbrowser KB253201

Back to the top

   

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please