There are all sorts of official ways of doing this implementing various IE hosting interfaces in order to handle the presentation of the UI. But if you’re trying to automate a web page and a pesky window.alert is blocking your progress, the following code will supress it for the current page.
/// <summary>
/// Handles the Navigated event of the browser control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="T:WebBrowserNavigatedEventArgs"/> instance containing the
/// event data.</param>
private void browser_Navigated( object sender, WebBrowserNavigatedEventArgs e )
{
if ( browser.Document != null ) {
// kills messagebox functionality by disabling the "window.alert()" function
object window = browser.Document.Window.DomWindow;
if ( window != null ) {
Type windowType = window.GetType();
BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance;
string[] args = { "window.alert=function() {};", "JScript" };
windowType.InvokeMember( "[DispID=1165]", flags, null, window, args );
} // if
} // if
}
3 Responses to “Easy way to prevent WebBrowser control from using window.alert”
Comments (2) Pingbacks (1)
do you have that code in VB?
This was a huge help to me!!!!! Thanks!!!