WebBrowser control and JavaScript errors

Asked
Active3 hr before
Viewed126 times

4 Answers

controljavascript
90%

What I would do is assign an object to webbrowser.ObjectForScripting and then inject a javascript function that assigns windown.onerror to a wrapper that calls the external script in the host app. Like: ,I know I can suppress the scripts errors, but I want them to run correctly, because they affect the rendering and the functionality of the page.,Connect and share knowledge within a single location that is structured and easy to search., 3 from question: "I know I can suppress the scripts errors, but I want them to run correctly, because they affect the rendering and the functionality of the page." – melya Jul 5 '17 at 10:19

What I would do is assign an object to webbrowser.ObjectForScripting and then inject a javascript function that assigns windown.onerror to a wrapper that calls the external script in the host app. Like:

window.onerror = function(message, url, lineNumber) {
   window.external.errorHandler(message, url, lineNumber);
}
load more v
88%

Hi, I am a web designer and one of my clients has embedded a WordPress site into his desktop application using the .net webbrowser control. Of course, this produces javascript errors when accessing the website from with that application. In the past I resolved this issue by doing 2 things after every theme update: (1) Put the following code in the site's .htaccess :,Heres the code, it should be loaded before any other javascript libraries:,empty_other, thanks for the tip. This may be a dumb question, but here goes: Is this something I could set up on shared hosting? Does it affect past the domain?,The repo has a decent tutorial and sample applications in both Winforms and WPF, and I've found that it works quite well.

Hi, I am a web designer and one of my clients has embedded a WordPress site into his desktop application using the .net webbrowser control. Of course, this produces javascript errors when accessing the website from with that application. In the past I resolved this issue by doing 2 things after every theme update: (1) Put the following code in the site's .htaccess :

<FilesMatch "\.(htm|html|php)$">
   <IfModule mod_headers.c>
      BrowserMatch MSIE ie
      Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
   </IfModule>
</FilesMatch>

(2) Put the following code in the site's header.php

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
load more v
72%

Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. ,Where I Am: Member Photos,Browse Topics> ,Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.

private static void MainBrowser_LoadCompleted(object sender, NavigationEventArgs e) {
   var wbMain = sender as WebBrowser;

   SetSilent(wbMain, true); // make it silent
}

private static void SetSilent(WebBrowser browser, bool silent) {
      if (browser == null)
         throw new ArgumentNullException("browser");

      // get an IWebBrowser2 from the document
      IOleServiceProvider sp = browser.Document as IOleServiceProvider;
      if (sp != null) {
         Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
         Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");

         object webBrowser;
         sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
         if (webBrowser != null) {
            webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] {
               silent
            });
         }
      }
   }
   [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IOleServiceProvider {
   [PreserveSig]
   int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
}
65%

This is the code that really solves the problem, the key is creating a IHTMLScriptElement with the script and injecting into the Head of the page, this effectively disables the javascript errors. I’ve not fully tested with a lot of sites to verify that is able to intercept all errors, but it seems to work very well with a lot of links that gave us a lot of problems in the past.,This is the very same script of the previous article, then I handle the Navigated event.,Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here. ,Opinions expressed by DZone contributors are their own.


<script type="text/javascript">

 function noError(){return true;}

 window.onerror = noError;

</script>

load more v

Other "control-javascript" queries related to "WebBrowser control and JavaScript errors"