none
How can I stop a script error popping up when using the webbrowser component

    Question

  • By clicking on this post you thought this was going to be an easy answer, if only that was the case :)

    I'm putting together a webbrowser and I thought I had all the bugs sorted out until someone showed me a scripting popup box within our corporate website.

    If I take a step back all I need to achieve is

    ·          All scripting errors are hidden from the user

    ·          Webpage login box’s are presented to the user (required for access to some external vendors)

    So I’m using the .Net Webbrowser and I know I need scripterrorsuppressed set to false as if it is set to true then I won’t see any login boxes (or will I, can you filter this?)

    I have hooked into the Document.error event to trap normal script popup boxes and set handle to true, this works most of the time. But the issue I having is that this event is not being triggered for this scripting error, I know my code works in general as if I browse around the web I can see this event being triggered, just not on this error. I think the issue is that it is a frame and hence the Document.error isn’t being triggered.

    Has anyone else had this problem where Document.error didn’t prevent all your scripting error boxes popping up?

    Right now the only site I can replicate this on is my our corporate site, I’ll see if I can find a public site that I can also replicate this error on and post the link.

    Also do I now need to think outside the box and not try to prevent the script errors poping up by hide the popbox e.g. visible = false or even kill them on title, any thoughts on this?

     

            public Form1()

            {

                InitializeComponent();

     

     

                webBrowser1.ScriptErrorsSuppressed = false;

                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

                webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);

     

              

            }

     

            void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)

            {

                ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);

     

            }

     

     

            private void textBox1_KeyDown(object sender, KeyEventArgs e)

            {

                if (e.KeyCode == Keys.Return)

                {

                     webBrowser1.Navigate(((TextBox)sender).Text);

                }

     

            }

     

           

            private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);

     

            }

     

     

            private void Window_Error(object sender, HtmlElementErrorEventArgs e)

            {

                // Ignore the error and suppress the error dialog box.

                e.Handled = false;

            }

     

     

     

    After a bit of surfing with the above code the only website that kind of produced the same error was http://entertainment.msn.co.nz/tv-guide?starttime=Sat Jul 31 09:11:39 UTC+1200 2010&channelset=1 , it proceduces a different error but it still bypasses the error event on one of the script errors, so the question is how is it triggered if it isn't being triggered in the error event. 

     

    The error I'm getting is due to the value not being defined eg Error 'sys' is undefined

    Friday, July 30, 2010 7:43 PM

Answers

  • Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink.aspx for sample code of navigation error handling.

    To handle script errors you need to implement IOleCommandTarget on the host and pass it to the browser via an overridden CreateWebBrowserSiteBase method 



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Harry Zhu Thursday, August 05, 2010 6:30 AM
    Sunday, August 01, 2010 4:59 AM
  • Thanks Sheng, I also went down the track of the error handing before I reliased you had responded, all in all I'm happy with the navagation now.

    For someone else doing this the track is to have 2 document completed events, one for good web pages and a second the you trigger for bad web pages, I have included my code to help the next person out.

     

     

           public Main()

            {

               

                browser = new WebBrowser2(); //web browser from the MSDN sample code

                this.Controls.Add(browser);

                browser.ScriptErrorsSuppressed = true;

     

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

                browser.NavigateError +=new WebBrowserNavigateErrorEventHandler(browser_NavigateError);       

            }

     

            private void browser_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)

            {

                if (browser.ScriptErrorsSuppressed)

                {

                    browser.DocumentCompleted -= browser_DocumentCompleted;

     

                    browser.ScriptErrorsSuppressed = false;

                    browser.Stop();

                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted_Error);

                }

            }

     

            private void browser_DocumentCompleted_Error(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

     

                browser.DocumentCompleted -= browser_DocumentCompleted_Error;

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

     

                browser.Navigate(browser.Url.ToString());

     

                CommonBrowserDocumentCompletedFuctions();

     

            }

     

            private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs args)

            {

                browser.ScriptErrorsSuppressed = true;

                CommonBrowserDocumentCompletedFuctions();

            }

     

     

    • Proposed as answer by DK. Da Tuesday, August 03, 2010 2:29 AM
    • Marked as answer by Harry Zhu Thursday, August 05, 2010 6:30 AM
    Tuesday, August 03, 2010 1:53 AM

All replies

  • Lets flip this on its head a little, does anyone know a good way to trip the error status from the webbrowser like 401 405 etc. Mos tthe posts talk about httpwebrequest but unsure how to closly link this to the webbrowser control.

    I'm now thinking suppressing every thing and if a page is not found (e.g. login box didn't pop up) then turn off the suppress and reload the page.  For this I need to be able to trap the failed pages and it would be great if I could do this before the user sees the error on the webcontrol.

    I don't what to use text as what is to say Microsoft changes it text dscriptions in the next release of IE.

     

     

     

    Friday, July 30, 2010 10:46 PM
  • Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink.aspx for sample code of navigation error handling.

    To handle script errors you need to implement IOleCommandTarget on the host and pass it to the browser via an overridden CreateWebBrowserSiteBase method 



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Harry Zhu Thursday, August 05, 2010 6:30 AM
    Sunday, August 01, 2010 4:59 AM
  • Thanks Sheng, I also went down the track of the error handing before I reliased you had responded, all in all I'm happy with the navagation now.

    For someone else doing this the track is to have 2 document completed events, one for good web pages and a second the you trigger for bad web pages, I have included my code to help the next person out.

     

     

           public Main()

            {

               

                browser = new WebBrowser2(); //web browser from the MSDN sample code

                this.Controls.Add(browser);

                browser.ScriptErrorsSuppressed = true;

     

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

                browser.NavigateError +=new WebBrowserNavigateErrorEventHandler(browser_NavigateError);       

            }

     

            private void browser_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)

            {

                if (browser.ScriptErrorsSuppressed)

                {

                    browser.DocumentCompleted -= browser_DocumentCompleted;

     

                    browser.ScriptErrorsSuppressed = false;

                    browser.Stop();

                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted_Error);

                }

            }

     

            private void browser_DocumentCompleted_Error(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

     

                browser.DocumentCompleted -= browser_DocumentCompleted_Error;

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

     

                browser.Navigate(browser.Url.ToString());

     

                CommonBrowserDocumentCompletedFuctions();

     

            }

     

            private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs args)

            {

                browser.ScriptErrorsSuppressed = true;

                CommonBrowserDocumentCompletedFuctions();

            }

     

     

    • Proposed as answer by DK. Da Tuesday, August 03, 2010 2:29 AM
    • Marked as answer by Harry Zhu Thursday, August 05, 2010 6:30 AM
    Tuesday, August 03, 2010 1:53 AM