INFO: WebBrowser Control Visible Property Fires Onload Event (199155)



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

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:
<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 about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbDHTML kbFAQ kbieObj kbinfo kbWebBrowser KB199155