none
Error Event not firing in WebBrowser when using FEATURE_BROWSER_EMULATION

    Question

  • If I create a simple form with a WebBrowser control, it fires the Window.Error event, on a Javascript error.

    When I add HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION for my application, it stops firing.

    It doesn't matter what value I use, even 0 will stop it working. In fact any method of Script Error handling, including injecting a javascript onerror statment using ExecScript, doesn't fire on an error.

    If I remove the DWord from FEATURE_BROWSER_EMULATION, it works again.

    Public Class Form1
    	Private WithEvents Win As HtmlWindow
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		WebBrowser1.Navigate("aimcosoftware.co.uk/test.html")
    	End Sub
    
    	Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    		Win = WebBrowser1.Document.Window
    	End Sub
    
    	Private Sub Win_Error(sender As Object, e As HtmlElementErrorEventArgs) Handles Win.Error
    		e.Handled = True
    	End Sub
    End Class


    Tuesday, January 02, 2018 10:43 PM

All replies

  •  Are you sure the Navigated event is not being raised,  it seems to be raised when i emulate the highest IE version i have installed.

    Put a Break Point on the line of code in that event and try it again,  see if it hits the Break Point.

     Also,  are you sure you have set the Value name of the registry key to your application's Assembly Name with the ".exe" extension on it too?  Are you also sure that you have set it's value to the correct value for the version you are emulating?  What have you used for these and what version of IE do you have installed?

    PS - If you are running your app from Visual Studio,  you will also need to add a registry key that has "TheAssemblyName.vshost.exe" which is what is being run when you are running it from Visual Studio.

     Perhaps try this code to make sure it is set correctly and try it.

    Imports Microsoft.Win32

    Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetEmulationToInstalledIEVersion() End Sub Private Sub SetEmulationToInstalledIEVersion() Dim VersionCode As Integer Dim Version As String = "" Dim ieVersion As Object = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("svcUpdateVersion") If ieVersion Is Nothing Then ieVersion = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("Version") End If If ieVersion IsNot Nothing Then Version = ieVersion.ToString.Substring(0, ieVersion.ToString.IndexOf("."c)) Select Case Version Case "7" VersionCode = 7000 Case "8" VersionCode = 8888 Case "9" VersionCode = 9999 Case "10" VersionCode = 10001 Case Else If CInt(Version) >= 11 Then VersionCode = 11001 Else Throw New Exception("IE Version not supported") End If End Select Else Throw New Exception("Joe, you got something going very wrong in your registry") End If Dim AppName As String = My.Application.Info.AssemblyName Dim Root As String = "HKEY_CURRENT_USER\" Dim Key As String = "Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION" Dim CurrentSetting As String = CStr(Registry.CurrentUser.OpenSubKey(Key).GetValue(AppName & ".exe", "")) If CurrentSetting = "" OrElse CInt(CurrentSetting) <> VersionCode Then Registry.SetValue(Root & Key, AppName & ".exe", VersionCode, RegistryValueKind.DWord) Registry.SetValue(Root & Key, AppName & ".vshost.exe", VersionCode, RegistryValueKind.DWord) End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click WebBrowser1.Navigate("http://aimcosoftware.co.uk/test.html") End Sub Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated MessageBox.Show(e.Url.ToString) End Sub End Class



    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, January 03, 2018 2:09 AM
    Wednesday, January 03, 2018 1:02 AM
  • Thanks, I'm aware of all your points, but you haven't read the question.

    It's the Error Event on HTMLWindow that doesn't fire, not Navigated.

    Even onError from the ActiveXInstance or injected onerror in Javascript doesn't fire.

    Wednesday, January 03, 2018 10:00 AM
  • Thanks, I'm aware of all your points, but you haven't read the question.

    It's the Error Event on HTMLWindow that doesn't fire, not Navigated.

    Even onError from the ActiveXInstance or injected onerror in Javascript doesn't fire.

     Well,  i could not tell from your post if you meant that the Navigated event stopped working which caused the Error event to stop working,  or if it was just the Error event that stopped working.

     Anyways,  both events still seem to work on my end so,  i am not sure what to suggest other than trying a new form project with the code below and see if it works in the new project.  If it does,  it is a problem with your application.  If it does not,  i would say that is more of an OS or IE problem.

     I used this in a new form project with one Button and one WebBrowser control added to the form.

    Imports Microsoft.Win32
    
    Public Class Form1
        Private WithEvents Win As HtmlWindow
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SetEmulationToInstalledIEVersion()
        End Sub
    
        Private Sub SetEmulationToInstalledIEVersion()
            Dim VersionCode As Integer
            Dim Version As String = ""
            Dim ieVersion As Object = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("svcUpdateVersion")
            If ieVersion Is Nothing Then
                ieVersion = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("Version")
            End If
            If ieVersion IsNot Nothing Then
                Version = ieVersion.ToString.Substring(0, ieVersion.ToString.IndexOf("."c))
                Select Case Version
                    Case "7"
                        VersionCode = 7000
                    Case "8"
                        VersionCode = 8888
                    Case "9"
                        VersionCode = 9999
                    Case "10"
                        VersionCode = 10001
                    Case Else
                        If CInt(Version) >= 11 Then
                            VersionCode = 11001
                        Else
                            Throw New Exception("IE Version not supported")
                        End If
                End Select
            Else
                Throw New Exception("Joe, you got something going very wrong in your registry")
            End If
    
            Dim AppName As String = My.Application.Info.AssemblyName
            Dim Root As String = "HKEY_CURRENT_USER\"
            Dim Key As String = "Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
            Dim CurrentSetting As String = CStr(Registry.CurrentUser.OpenSubKey(Key).GetValue(AppName & ".exe", ""))
            If CurrentSetting = "" OrElse CInt(CurrentSetting) <> VersionCode Then
                Registry.SetValue(Root & Key, AppName & ".exe", VersionCode, RegistryValueKind.DWord)
                Registry.SetValue(Root & Key, AppName & ".vshost.exe", VersionCode, RegistryValueKind.DWord)
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Navigate("http://aimcosoftware.co.uk/test.html")
        End Sub
    
        Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
            Win = WebBrowser1.Document.Window
            Me.Text = "Navigated... "
        End Sub
    
        Private Sub Win_Error(sender As Object, e As HtmlElementErrorEventArgs) Handles Win.Error
            e.Handled = True
            Me.Text &= "Error Handled... "
        End Sub
    End Class
    
     

     Here you can see it sets my form's text as the events are fired...


    If you say it can`t be done then i`ll try it

    Wednesday, January 03, 2018 8:07 PM
  • Thanks for your time, Error does not fire one when emulation is set.

    I'll see if it's the same on Windows 10.

    Wednesday, January 03, 2018 9:47 PM
  • Hi Andrew,

    Have you solved your issue now? If yes, please remember to close this thread by marking the helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 22, 2018 9:05 AM
    Moderator
  • I haven't solved the issue, it is still doing it in my application, but I have found a workaround.

    I haven't posted it because it may not work for anyone else.

    Monday, January 22, 2018 9:58 AM