INFO: WebBrowser Control Visible Property Fires Onload Event

ID: Q199155


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5


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:


<HTML>
<HEAD>
<TITLE>Test Page</TITLE>

<SCRIPT LANGUAGE="VBScript">
   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
</SCRIPT>
</HEAD>

<BODY onload="Load" onbeforeunload="BeforeUnload" onunload="UnLoad">
Test Page
</BODY>
</HTML> 


REFERENCES

For more information, please see the MSDN Web Workshop:

http://msdn.microsoft.com/workshop/default.asp

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Mark Davis, Microsoft Corporation

Additional query words: kbDSupport onload onunload

Keywords : kbDHTML kbIE kbIE400 kbIE401 kbIEObj kbWebBrowser kbIE401sp1 kbIE500dp1 kbIE401sp2 kbGrpInet kbDSupport kbIEFAQ
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: January 27, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.