Has all my video tutorials usually with source code in an organized cyber-library. This video we are introduced to the WebB.
Occasionally I need to embed HTML in my applications. If it isjust to display some simple layout with basic interactions, Imight use a component such as HtmlRenderer. In most caseshowever, I need a more complex layout, JavaScript or I mightwant to display real pages from the internet - in which case I'mlumbered with the WebBrowser
control.
I'm aware other embeddable browsers exist, but the idea ofshipping additional multi-MB dependencies doesn't make senseunless an application makes heavy use of HTML interfaces
The WebBrowser
control annoys me in myriad ways, but it doesget the job done. One of the things that occasionally frustratesme is that by default it is essentially an embedded version ofInternet Explorer 7 - or enabling Compatibility Mode in a modernIE session. Not so good as more and more sites use HTML5 andother goodies.
Apple app store download zoom. Rather fortunately however, Microsoft provide the ability toconfigure the emulation mode your application will use. It's notas simple as setting some properties on a control as it involvessetting some registry values and other caveats, but it is stilla reasonable process.
The table below (source) lists the currently supportedemulation versions at the time of writing. As you can see, it'spossible to emulate all 'recent' versions of Internet Explorerin one of two ways - either by forcing a standards mode, orallowing !DOCTYPE
directives to control the mode. Theexception to this dual behaviour is version 7 which is as is.
According to the documentation the IE8 (8000) and IE9 (9000) modes will switch to IE10 (10000) mode if installed. The documentation doesn't mention if this is still the case regarding IE11 so I'm not sure on the behaviour in that regard.
Value | Description |
---|---|
11001 | Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the !DOCTYPE directive. |
11000 | IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11. |
10001 | Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive. |
10000 | Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10. |
9999 | Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive. |
9000 | Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9. |
8888 | Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive. |
8000 | Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8 |
7000 | Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control. |
Setting the emulation version is very straightforward - add avalue to the registry in the below key containing the name ofyour executable file and a value from the table above.
Note: If you do this from an application you're debuggingusing Visual Studio and the Visual Studio Hosting Processoption is enabled you'll find the executable name may not bewhat you expect. When enabled, a stub process with a slightlymodified name is used instead. For example, if yourapplication is named calc.exe,
you'll need to add the valuecalc.vshost.exe
in order to set the emulated version for thecorrect process.
As it makes more sense to detect the version of IE installed onthe user's computer and set the emulation version to match,first we need a way of detecting the IE version.
There are various ways of getting the installed IE version, butthe sensible method is reading the value from the registry aseverything else we are doing in this article involves theregistry in some fashion.
Older versions of IE used the Version
value, while newerversions use svcVersion
. In either case, this value containsthe version string.
We can use the following version to pull out the major digit.
int
with the major version component rathera Version
class. In this example, I don't need a fullversion to start with and it avoids crashes if the versionstring is invalidSecurityException
and UnauthorizedAccessException
exceptions which will be thrown if the user doesn't havepermission to access those keys. Again, I don't really wantthe function crashing for those reasons.You can always remove the try
block to have all exceptionsthrown instead of the access exceptions being ignored.
The functions to get and set the emulation version are usingHKEY_CURRENT_USER
to make them per user rather than for theentire machine.
First we'll create an enumeration to handle the differentversions described above so that we don't have to deal withmagic numbers.
Next, a function to detect the current emulation version in useby our application, and another to quickly tell if an emulationversion has previously been set.
And finally, we need to be able to set the emulation version.I've provided two functions for doing this, one which allows youto explicitly set a value, and another that uses the bestmatching value for the installed version of Internet Explorer.
As mentioned previously, I don't really want these functionscrashing for anticipated reasons, so these functions will alsocatch and ignore SecurityException
andUnauthorizedAccessException
exceptions. TheSetBrowserEmulationVersion
function will return true
if avalue was updated.
If you just want 'fire and forget' updating of the browseremulation version, you can use the following lines.
This will apply the best matching IE version if an emulationversion isn't set. However, it means if the user updates theircopy if IE to something newer, your application will potentiallycontinue to use the older version. I shall leave that as anexercise for another day!
While experimenting with this code, I did hit a major caveat.
In the original application this code was written for, I wasapplying the emulation version just before the first windowcontaining a WebBrowser
control was loaded, and this workedperfectly well.
However, setting the emulation version doesn't seem to work ifan instance of the WebBrowser
control has already been createdin your application. I tried various things such as recreatingthe WebBrowser
control or reloading the Form
the control washosted on, but couldn't get the new instance to honour thesetting without an application restart.
The attached demonstration program has gone with the 'restartafter making a selection' hack - please don't do this inproduction applications!
You should carefully consider where or not to change theemulation version of your application. If it's currently workingfine, then it's probably better to leave it as is. If however,you wish to make use of modern standards compliant HTML, CSS orJavaScript then setting the appropriate emulation version willsave you a lot of trouble.
The are a lot of different options you can apply to InternetExplorer and the WebBrowser
control. These options allow youto change behaviours, supported features and quite a few more.This article has touched upon one of the more commonrequirements, but there are a number of other options that areworth looking at for advanced application scenarios.
An index of all available configuration options can be found onMSDN.
Filename | Description | Version | Release Date |
---|---|---|---|
IeBrowserEmulation.zip
| Sample file for the article configuring the emulation mode of an Internet Explorer WebBrowser control | 28/06/2014 | Download |
While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?
Great... Its Working Smoothly.. Thank you for your Post
Very Thanks. Works perferctly.
the usage command 'if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet()) { InternetExplorerBrowserEmulation.SetBrowserEmulationVersion(); }'
flags the 'InternetExplorerBrowserEmulation' as cannot resolve. Could you tell me what am doing wrong. All the other code has no errors
I at all and thanks for your code. It works fine for me but I have a little problem: when my app starts, IE show me a security message about the scripting ('to facilitate the security has been prevented to the file....'). How can I avoid this problem?
Your article is unusual in that it is intelligently written and professionally presented - well done.
An additional (well known) issue with embedded IE is JS pop up errors. If you had time, a mention of this might be a useful extension to your excellent guide - you might mention the idea suggested by Ashot Muradian at http://stackoverflow.com/questions/6138199/wpf-webbrowser-control-how-to-supress-script-errors/18289217#18289217
Thank you.
Great solution! Thank you very much.
Thank you very much, It helped me
Hey!, thanks for this code, i have a question.. all of you tried to show a pdf file inside webbrowser with this code??,.
i cant show a pdf inside of webbrowser, the webbrowser download the file and display on adobe reader desktop app, but without set the emulation mode it shows inside webbrowser.
This seems to have been fixed in WPF, so instead of using Windows Forms try a WPF project.
i got the message ' Failed to update browser emulation version .. Why ? and how can resolve it
This demo application is not working for me, js script error message always show for each browser version selection.
Thank you Richard for the code. Do I understand right that the webbrowser control can only emulate a browser that is installed locally? I have a client who has IE 9 installed on all his machines - would using the webbrowser control emulating IE10 or 11 put him into place to access websites that require a higher version of IE, e.g. in order to use SVG features?
Thank you so much. My WebBrowser control show me version 11, but I get error. I spend for it all day. After your post it work. Thank you so much again
is it possible to make each emulation (IE9, IE10, IE11) use their own session cookie? now they share the same cookie with default Internet Explorer
Good Job!! Thank You
An excellent example, it is what I was looking for. Thank you for both an explanation and the sample! :-)
I've been doing this for a long time now and we recently upgraded to Windows 1809. This now blows up webpages in Outlook. Specifying 11001 or 11000 worked fine on Windows 1607. Now in Windows 1809 the pages act as if I opened IE and am going to the specified webpage using capability settings turned on. This naturally gives a garbled page. I'm wondering if there's new values I have to use above 11000. I can't seem to find official MS documention on this.
For IE11 in WIN10 you need to look at IE minor version as well. In WIN10 minor version is != 0 while in older OS it's 0. If that's the case, you should set 11001 as the emulation version (BrowserEmulationVersion.Version11Edge).
Авант Браузер - Avant Browser | Опера - Opera | Гугл хром - Google Chrome | Мозилла Фаерфокс - Mozilla Firefox | |
Описание | Стабильный многозадачный браузер | Быстрый и надежный независимый интернет-браузер. | Сверхбыстрый веб-браузер с беспрецедентным уровнем безопасности. | Сверхскоростной веб-браузер с десятками дополнений. |
---|---|---|---|---|
Рейтингу | ||||
Загрузки | 174 | 4,311 | 4,291 | 5,134 |
Цена | $ 0 | $ 0 | $ 0 | $ 0 |
Размер файла | 4.33 MB | 61.20 MB | 68.70 MB | 54.80 MB |
Техническая информация | |
Рейтингу: | 5(1945) |
Рейтинг в Интернет браузеры: | 202 |
Последняя оценка: | |
Лицензия: | Бесплатная |
Размер файла: | 0 |
Версия: | Windows 10 |
Последнее обновление: | 29/7/2015 |
Операционная система: | Windows XP, Windows Vista, Windows 8, Windows 7, Windows 10 |
Языки: | Русский, Испанский, Немецкий, Английский, Индонезийский, Итальянский, Португальский, Польский, Турецкий, Чешский, Датский, Шведский, Китайский, Иврит, Арабский, Французский, Финский, Корейский, Норвежский, Хинди, Голландский, Японский, Греческий, Вьетнамский Больше.. |
Разработчик: | Microsoft |
Всего скачали (Русский): | 251 |
Всего скачали (По миру): | 73,369 |