Hello everyone,

I've been trying to generate a way to catch or suppress errors in my web browser control. actually what i really need in my browser is that if i run my project and the page cannot be display, to generate an error handler so instead of showing "The page cannot be display" to show something else, like "about:blank" for example or not display anything. i tried this:

Dim WithEvents objDoc As MSHTML.HTMLDocument
Dim WithEvents objWind As MSHTML.HTMLWindow2
Dim objEvent As CEventObj

Private Sub WebBrowser1_NavigateComplete()
Set objDoc = WebBrowser1.Document
Set objWind = objDoc.parentWindow
End Sub

Private Sub objWind_onerror(ByVal description As String, ByVal URL As String, ByVal line As Long)
Set objEvent = objWind.event
objEvent.returnValue = True
MsgBox (description)
End Sub

But i would like something diferent or better than this.
Any help will be appreciate.
Thanks in advance

Dani AI

Generated

Short expert note: window.onerror only suppresses JavaScript runtime errors inside the page; it won't stop Internet Explorer from showing its built-in "The page cannot be displayed" error page. , was on the right track thinking about HTTP/transport errors, and 's event wiring is similar — but the most reliable place to catch transport/HTTP failures in the classic WebBrowser control is the NavigateError sink (DWebBrowserEvents2). Cancel the failed navigation there and load about:blank or a small local HTML snippet.

Example (VB6 / classic WebBrowser — add a reference to Microsoft Internet Controls):

' in the form that hosts the WebBrowser
Private Sub WebBrowser1_NavigateError(ByVal pDisp As Object, ByVal URL As Variant, ByVal Frame As Variant, ByVal StatusCode As Variant, ByRef Cancel As Boolean)
    ' treat HTTP status codes >= 400 as failures, cancel the default error page and show about:blank
    If Not IsNull(StatusCode) Then
        If CLng(StatusCode) >= 400 Then
            Cancel = True
            WebBrowser1.Navigate "about:blank"
            If Not WebBrowser1.Document Is Nothing Then
                WebBrowser1.Document.Write "<html><body style='font-family:Arial'>Page unavailable.</body></html>"
                WebBrowser1.Document.Close
            End If
        End If
    End If
End Sub

Notes and alternatives:

  • Script error dialogs are a different beast; set WebBrowser.Silent = True to suppress them. That does not stop navigation error pages.
  • In VB.NET's System.Windows.Forms.WebBrowser you normally do not get NavigateError. Two practical workarounds are: perform a quick HEAD/GET (HttpWebRequest) to verify the response before calling Navigate, or sink the COM NavigateError event with an advanced COM event-sink/subclass.
  • IE sometimes serves internal error pages from res:// — if NavigateError is not raised, detect those by inspecting Document.Location/URL or Document.Title and replace the content.

If NavigateError does not appear for your environment, post the host (VB6 vs VB.NET), IE version and a short snippet of how you create/use the control and someone can provide the exact sink/registration code.

Recommended Answers

All 4 Replies

You should be able to check for the HTTP response code, either via an exposed property/function return of the WebBrowser control itself, or manually in the page headers. If the response code is an error (say 400, page not found) you could navigate "about:blank" or whatever else you want to do.

Thanks, but i don't like your suggestion... i'll find another way arround this.

You can try the following, just change the code to suit your needs -

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)

Set objDoc = WebBrowser1.document
Set objWind = objDoc.parentWindow
End Sub

Private Sub objWind_onerror(ByVal Description As String, ByVal URL As String, ByVal line As Long)

Set objEvent = objWind.event

objEvent.returnValue = True
End Sub

Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)

Dim CtrlKey

If CtrlKey <> True Then
    Cancel = True
End If

Cancel = True
End Sub

Useless code, thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.