Microsoft KB Archive/199155

= INFO: WebBrowser Control Visible Property Fires Onload Event =

Article ID: 199155

Article Last Modified on 5/11/2006

-

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 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0

-



This article was previously published under Q199155



SUMMARY
When the Internet Explorer WebBrowser control is hosted by an application (a Visual Basic form, for example), the Internet Explorer DHTML window.onunload and window.onload events are fired on the page if you change the visible property.

This behavior is by design.



MORE INFORMATION
Before a Web page unloads, an onbeforeunload event is fired. If the visible property has changed, the event fires but the page still loads, so you can set a flag in that event handler to determine when the final onunload event actually fires (indicating that the page is going away, not just that the visible property has changed).

The following script from the HTML source demonstrates using this method to determine the first onload event and the final onunload event for the page:   Test Page

 Dim bLoaded Dim bUnLoaded bLoaded = false bUnLoaded = false

Sub Load If bLoaded Then 'This indicates the event could be the result 'of a change to the visible property of the 'containing WebBrowser control Exit Sub Else 'This is the true onLoad event for the page bLoaded = true MsgBox "First onLoad" End If  End Sub

Sub Unload If Not bUnLoaded Then 'Again, this indicates we're being fired as      'a result of a change to the visible property Exit Sub Else 'This is the final unload of the page MsgBox "Final onUnload" End If  End Sub

Function BeforeUnload bUnLoaded = true 'If this is not the final unload of the page 'then the setTimeout expression will happen. 'If this is the final unload, the page will 'be gone before the setTimeout expression 'occurs. setTimeout "bUnLoaded = false", 0, "vbscript" End Function  

 Test Page  

