WebBrowser script error reason and Web Browser Control & specifying the IE Version

Source: Internet
Author: User

  recently wanted to use the  webbrowser control to write a program to visit China Southern power grid, in order to inquire about the power outage plan of the various outlets information, but found that the script error, resulting in no normal access, and IE access does not have this problem. Beginning to hold a headache medical head, foot pain medical foot mentality, thinking how to solve the problem of script error, can not solve after thinking, ie no problem, that is the problem of WebBrowser itself, thought had seen before, said WebBrowser's work mode is not installed in the normal mode of IE, Instead of working in protected mode, to want to work in native IE mode, you need to modify the registry.   report script errors, Regardless of whether you click "Yes" or "no", you will not be able to access related features, such as "power outage Queries". 32-bit system running 32-bit programs or 64-bit systems running 64-bit programs, Modified here: 32-bit program under 64-bit system, hkey_local_machine\software\wow6432node\microsoft\internet explorer\main\ Featurecontrol\feature_browser_emulation add own application DWORD key, value changed to 2AF9. Now it's OK to visit China Southern power grid, but it's also a little strange that the click [Login] button out of the interface is blank, unable to log in, click [Login now] can login. Here are the articles to go: Web Browser Control & specifying the IE Version

updated 26th, 2016-the original was published in May 20

I Use the Internet Explorer Web Browser Control under a lot of my desktop applications to display document content. For example Markdown Monster, help Builder and Websurge rely heavily on the Web Browser Control to render their document C Entric or even rich interactive UI (in the case of Markdown Monster which, hosts an HTML editor). Whether you ' re just rendering document content, or you ' re interacting with rich interactive content, HTML happens to is on E of the most common document formats to display or interact with and it makes a wonderful addition to conventional forms Based UI. Even in desktop applications, is often the easier than using labels or edit boxes or even some of the WPF text containers. HTML is easy to generate, generally re-usable, and easily extensible and distributable. The Web Browser Control allows for a effective-of-the-display HTML in your applications in a-a-to-blends in and becom Es part of your application.

But there's a snag:the Web Browser Control is-by default-perpetually stuck in IE 7 rendering mode. Even though we ' re now up to IE one and a reasonably HTML5 compatible browser, the Web browser Control always uses the IE 7 Rendering engine by default. This was because the original versions of the ActiveX control used this mode and for backwards compatibility the control CO Ntinues this outdated and very HTML5 unfriendly default.

This applies whether your ' re using the Web Browser control in a WPF application, a WinForms app, or FoxPro application usin G The ActiveX control. Behind The scenes all these UI platforms use the same COM interfaces and so your ' re stuck with those same rules.

The good news is there be a couple of ways to override the default rendering behavior:

    • Using the IE X-UA-Compatible Meta header
    • Using Application specific FEATURE_BROWSER_EMULATION Registry Keys

But first lets see the problem more graphically.

Rendering challenged

To see what I ' m talking about, here is the shots rendering an HTML5 page, includes some CSS3 functionality–r ounded Corners and Border Shadows-from an earlier post. One uses Internet Explorer as a standalone browser, and one uses a simple WPF form that includes the Web browser control.

Full IE browser:                                                                        Web Browser Control in a WPF form:         

The full Internet Explorer, the page displays the HTML correctly–you see the rounded corners and shadow displayed. Obviously the latter rendering using the Web Browser control in a WPF application is a bit lacking. Not only is the new CSS features missing but the page also renders in Internet Explorer's quirks mode so all the margins, padding etc. behave differently by default, even though there ' s a CSS reset applied on the This page. But the default IE 7 mode doesn ' t recognize many of these settings resulting in a terrible render mode.

If you ' re building an application that intends to use the Web Browser control for a live preview of some HTML this is Clea Rly undesirable.

Using the x-ua-compatible HTML Meta Tag

If you control the content in your Web Browser control by rendering the HTML pages of you display yourself, the easiest the T o provide later versions of the IE rendering engine is by using the IE Edge mode header. By adding a meta tag to the head of the HTML document rendered in the Web Browser Control you can effectively override the IE Rendering engine and specify which version of IE (or the latest version) to use.

The tag to use the header of the looks like this:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

Inside of a full document it looks like this:

<!DOCTYPE html>

Note the header should be the first and so, the engine is applied before any other headers is processed.

In this case uses the current version of IE which is installed on the machine IE=edge . So if you have IE one installed that's used, if IE is installed that ' s used.

You can also specify a specific version of IE:

<meta http-equiv="X-UA-Compatible" content="IE=10" />

For info in the modes available see this stackoverflow answer.

Alternately can also serve X_UA_Compatible: IE=edge as a raw HTTP header from a WEB server, which have the same behavior as the http-equiv meta Header.

Caveats with the Edge Mode Header

There is a few things you need on order to use the META tag and make it work properly:

  • you had to control the Page in order to add the <meta> tag The page so, can add the <meta> tag into your HTML. If you ' re rendering arbitrary HTML that doesn ' t include the tag, then this approach won ' t work obviously. Typically the header approach works great if you generate your own content.

  • Browser Version Reporting is incorrect <meta>the tag changes the rendering engine behavior that IE uses and it doesn ' t change the the-the-IE reports its version . If You access pages This does IE version checking you'll find the it still points at IE 7 (or sometime some custom browser Version) that's doesn ' t reflect the current rendering mode. If you ' re running script code, rely on browser sniffing this can become a problem. I ran into this recently with Ace Editor, which have a couple of odd places where it uses browser sniffing for dealing with The clipboard, and that code wouldn ' t work. This was likely an edge (ha-ha) case, but was aware that this can become a problem.

Feature Delegation via Registry Hacks

Another and perhaps more robust-affect the Web Browser Control version is by using FEATURE_BROWSER_EMULATION . Starting with IE 8 Microsoft introduced registry entries this control browser behavior when a Web browser control is embed Ded to other applications. These registry values is used by many application on your system.

Essentially can specify a registry with the name of your executable and specify the version of IE so you would like To load. The numbers is specified as 11000, 10000, 9000, 8000 and 7000. The keys can be specified either for the current user (HKCU) or globally for all users (HKLM).

Here's what I had in my HKCU key:

Notice some big applications like Visual Studio and Outlook use these overrides and at the HKLM keys you'll also find AP PS like Skype SnagIt, Fiddler, Sourcetree, 1Password and the "Windows help Viewer" to name a few. So this feature are actually used by a wide range of popular software.

Registry Key location for Feature_browser EMULATION

You can specify these keys on the registry at:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

The HKCU key is the best place to set these values because there's a single key and it can be set without admin rights, BU T can also set these keys at the machine level at HKLM:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Or for a, application on a, bit machine:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

Key Name

The KeyName is the EXE name of your application like:

    • Outlook.exe
    • MarkdownMonster.exe
Values

The value specifies the IE version as follows:

The value to set of this key to be (taken from MSDN here) as decimal values:

11001 (0X2AF9) Internet Explorer 11. Webpages is displayed in IE11 standards mode, regardless of the! DOCTYPE directive.

11000 (0X2AF8) Internet Explorer 11. Webpages containing standards-based! DOCTYPE directives is displayed in IE9 mode.

10001 (0X2AF7) Internet Explorer 10. Webpages is displayed in IE10 standards mode, regardless of the! DOCTYPE directive.

10000 (0x2710) Internet Explorer 10. Webpages containing standards-based! DOCTYPE directives is displayed in IE9 mode.

9999 (0x270f) Internet Explorer 9. Webpages is displayed in IE9 standards mode, regardless of the! DOCTYPE directive.

9000 (0x2328) Internet Explorer 9. Webpages containing standards-based! DOCTYPE directives is displayed in IE9 mode.

8888 (0X22B8) Webpages is displayed in IE8 standards mode, regardless of the! DOCTYPE directive.

8000 (0X1F40) Webpages containing standards-based! DOCTYPE directives is displayed in IE8 mode.

7000 (0x1b58) Webpages containing standards-based! DOCTYPE directives is displayed in IE7 standards mode. This mode is kind of pointless since it ' s the default.

Setting These keys enables your applications to use the latest Internet Explorer versions on your machine easily. Unfortunately there doesn ' t seem to being a key that says use the latest version that ' s installed-you has to be specific R Egarding the version unfortunately. Given that Windows 7 and later can run IE one by one, I ' m requiring users to having IE one if I want to use HTML5 and more advanced C SS features like Flexbox, but if your content are simpler you can probably get away with using IE ten or even IE 9.

Don ' t forget to add Keys for Host environments

As mentioned above you has to specify the name of the The EXE you ' re running as the key in the registry.

If you ' re using a development environment like visual Studio or Visual FoxPro to debug your applications, keep in mind tha T your main EXE you is running while debugging is not being the final EXE that is building. So after your ' ve set up your registry keys, your debugging experience may still don't see the enhanced rendering features in The Web Browser control because you ' re not actually running the final EXE.

The problem is if you have debug in these environments, you are running a debugging host and in order for it to render P Roperly the host to the keys as well FEATURE_BROWSER_EMULATION .

For Visual Studio This would be yourapp.vshost.exe , which is the debugging host. For Visual FoxPro you ' ll want vfp9.exe . Simply add these keys to the registry alongside those, live your with for actual application.

Registry Key installation for your application

It's important to remember that the registry settings from above be made per application, so most likely this is somethin G you want to set up with your installer. I Let my installers write the values to the registry during the install process which also removes the keys on uninstall .

Personally I always prefer setting this value per user using using the HKCU key.

IF you set the keys globally on HKLM for all users, remember A and a bit settings require separate settings in the Registry. So if you ' re creating your installer your most likely would want to set both keys in the registry preemptively for your APPL Ication.

I use Tarma Installer for all of my application installs and Tarma I Configure registry keys for both and set a flag to Only install the latter key group in the version:

Summary

It would be nice if the Web Browser Control would just use the native Internet Explorer engine as are, but as you see in th Was article, that's unfortunately not the case and you had to coerce the browser. If you have control over your documents, the render in the Web Browser control, you are able to use X_UA_Compatible header in Your pages. Otherwise you has to resort to the registry hack using FEATURE_BROWSER_EMULATION .

Personally I ' ve used the registry hack for all of my apps this use the Web Browser Control because my applications tend to Render HTML from all sorts of different sources-local generated content, as well as Web loaded pages for previews and S Ometimes even dynamically injected content. It's better to force the latest IE version of all content than forget the fact that need custom headers for other NON-APPL Ication content you might display (Update notices, registration forms, notifications etc)

These days most machines'll be running either IE ten or one, so there's much less of a problem with Differening browser be Havior than there used to be.

Other Posts your might also like
    • Multi-targeting and Porting A. NET Library to. NET Core 2.0
    • Adding minimal OWIN Identity authentication to an Existing ASP application
    • Publishing and Running ASP. NET Core applications with IIS
    • upgrading to. NET Core 2.0 Preview

WebBrowser script error reason and Web Browser Control & specifying the IE Version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.