WebBrowser control and JavaScript errors
Solution 1
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);
}
Refere to: http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/
Solution 2
If you have IE9 installed, the WebBrowser will still use IE7 mode unless you override this behaviour with a registry setting - as described in this StackOverflow answer. This is the most likely cause of the JavaScript errors you're getting in the WebBrowser (because you're not seeing the same errors in IE9).
You can make the registry setting using the following c# code (which sets IE10 mode if Windows 8 is detected) and changing app-name.exe to match your own application. You should add an error handler for the case where there are insufficient privileges (admin privileges are required to write to this registry key).
string installkey = @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string entryLabel = "app-name.exe";
System.OperatingSystem osInfo = System.Environment.OSVersion;
string version = osInfo.Version.Major.ToString() + '.' + osInfo.Version.Minor.ToString();
uint editFlag = (uint)((version == "6.2") ? 0x2710 : 0x2328); // 6.2 = Windows 8 and therefore IE10
RegistryKey existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, false); // readonly key
if (existingSubKey.GetValue(entryLabel) == null)
{
existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, true); // writable key
existingSubKey.SetValue(entryLabel, unchecked((int)editFlag), RegistryValueKind.DWord);
}
Solution 3
You can use the following code line to get rid of those types of errors:
webBrowser1.ScriptErrorsSuppressed = true;
It will prevent getting JavaScript errors.
Solution 4
So i know the post is old, but it was a recent problem for me and i had to do some serious digging and thinking outside the box.
basically like most replies here - you cannot alter the webbrowser control to use the most recent IE engine. mine uses IE7 by default, i have seen some replies that basically changes/ adds stuff to registry, am always not comfy when it comes to the registry, a cleaner way to address this issue would be to append code on your website that forces it to use the most current IE engine on any pc and works like a charm.
if you have access to the web.config file of the page you intend to browse, simple append:
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-UA-Compatible" value="IE=edge" />
</customHeaders>
</httpProtocol>
</system.webServer>
and your site would force webbrowser control to run the most current IE engine on your computer. Other options are found here:
https://www.leapinggorilla.com/Blog/Read/1016/ie-ate-my-css---disabling-compatability-mode
I should state that this would only work if you have access to the page / web.config of the website/ application you are trying to access- which was my case.
Solution 5
THe WebBrowser
control uses IE7. So if there is a problem then your script does not work for IE7 and you will have to fix that.
You cannot integrate IE9 as it depends on it being installed on the computer and not everyone has IE9 installed.
Related videos on Youtube
user689792
Updated on March 08, 2020Comments
-
user689792 about 4 years
When I access the page with the browser (ie9), the browser is rendering ok.
When I use the WebBrowser control I have JavaScript errors.
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.
How can I solve this problem ? Can I integrate IE9 directly in the Windows Form and use similar methods like with the WebBrowser control (navigate,get id, invoke click) ?
Thanks.
-
user689792 about 13 yearsI have IE9 installed on my computer. I'm accessing an account that I have on a site. Using the webbrowser control I'm automating some operation in that account. In the webbrowser I get javascript errors that affect the functionality of the site. I don't get this errors in the IE browser. How can I find what browser version use the webBrowser controller ?
-
Oskar Kjellin about 13 years@user689792 As stated in answer, the control always uses IE7, no matter what you have installed
-
daniel over 11 yearsI did add these entries with my exe filename but i still get the JavaScript errors in the browser control. I don't get them in the browser (IE10).
-
pgfearo over 11 years@zoidbergi - try running xmlquire-we (once with admin privileges to set the registry changes) - and see if you get the same errors when you enter your URL into the browser control's address bar - downloadable from qutoric.com/xmlquire/ce
-
Jack almost 10 years@OskarKjellin: Actually, it does. You can use IE9 or the IE installed on machine.
-
Oskar Kjellin over 9 years@Jack That might've changed since 2011 ;)
-
Robert over 8 yearsThe default mode is IE7 but it can easily be changed by feature emulation mode in registry.
-
Valamas over 8 yearsI was using Jquery 2+. since using v1+ the errors disappeared. I only want the webbrowser control for a little task. So this worked for me.
-
melya almost 7 yearsfrom 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."
-
Kamil almost 5 yearsThis is not true. Take a look here: weblog.west-wind.com/posts/2011/May/21/…
-
Kamil almost 5 years@daniel You probably messed up something. It is even possible to make it work in Edge browser mode. Please read this article: weblog.west-wind.com/posts/2011/May/21/…
-
Oskar Kjellin over 4 years@Kamil it was true at the time of writing
-
Kamil over 4 years@OskarKjellin I see. Can you edit your answer a bit please? I can't undo my downvote without any edit in answer.
-
Sidso over 2 years@mgokhanbakal where to place the code? As it says you cant change it to true in designer or build. If I place it .cs file, it fails.
-
mgokhanbakal over 2 years@Sidso, you can add it to the place where you load the page.