Microsoft KB Archive/185128

= How To Insert Event Handler Into Web Page from WebBrowser App =

Article ID: 185128

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q185128



SUMMARY
When you are hosting the WebBrowser control in your applications, you may want to insert an event handler for a script event. This article describes how do that in a Visual Basic application.



MORE INFORMATION
There are three important points to note when trying to insert script for event handlers on your Web pages from your Visual Basic application.  Use the insertAdjacentHTML method that is new in Internet Explorer 4.0x. You can use only JavaScript because VBScript events are bound when the page is first parsed. You must insert HTML that causes the page to be reparsed. This HTML can also be hidden. This tag "span style='display:none'" causes the HTML to be hidden and reparsed. For additional information, please see the following article in the Microsoft Knowledge Base:

185140 PRB: Trouble Inserting Non-Displayable HTML into Web Page

 You must use the  tag. DEFER Indicates the script block contains only functions and no in-line script. Deferring the parsing of scripts until they are needed can improve performance by decreasing the time it takes to load a document.

Use the following steps to insert script for event handlers from a Visual Basic application:  Open a new Standard EXE project.</li> Add the WebBrowser control to your form.</li>  Add a Command Button and the following code: Option Explicit

Private Sub Command1_Click Dim str As String

' Insert some hidden HTML and the script str = " h " & _ "function document.onclick {alert(1);} "

WebBrowser1.Document.body.insertAdjacentHTML "BeforeEnd", str End Sub

Private Sub Form_Load WebBrowser1.Navigate "http://SomeServer/SomeWebPage.htm" End Sub </li> You can see if the script was inserted by using the outerHTML method like this:

Debug.Print WebBrowser1.Document.body.outerHTML

</li></ol>

<div class="references_section">