27

i am using automation (i.e. COM automation) to display some HTML in Internet Explorer (9):

ie = CoInternetExplorer.Create;
ie.Navigate2("about:blank");
webDocument = ie.Document;
webDocument.Write(szSourceHTML);
webDocument.Close();
ie.Visible = True;

Internet Explorer appears, showing my html, which starts off as:

<!DOCTYPE html>
<HTML>
<HEAD>
   ...

Note: the html5 standards-mode opt-in doctype html

Except that the document is not in ie9 standards mode; it's in ie8 standards mode: alt text


If i save the html to my computer first:

alt text

and then view that html document, IE is put into standards mode:

alt text

My question is how update my SpawnIEWithSource(String html) function to throw the browser into standards mode?

void SpawnIEWithSource(String html)
{
   Variant ie = CoInternetExplorer.Create();
   ie.Navigate2("about:blank");
   webDocument = ie.Document;
   webDocument.Write(html);
   webDocument.Close();
   ie.Visible = true;
}

Edit: A more verbose, less understandable or readable code sample, that doesn't help further the question might be:

IWebBrowser2 ie;
CoCreateInstance(CLASS_InternetExplorer, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_WebBrowser2, ie);
ie.AddRef();
ie.Navigate2("about:blank");

IHtmlDocument doc;
dispDoc = ie.Document;
dispDoc.AddRef();
dispDoc.QueryInterface(IHTMLDocument2, doc);
dispDoc.Release()
doc.Write(html); 
doc.Close();
doc.Release();
ie.Visible = true;
ie.Release();

Update

Commenter asked on the ieblog entry Testing sites with Browser Mode vs. Doc Mode:

Can we get a description of how the document mode is determined when the HTML content is within an embedded webcontrol? Seems to be that the document mode is choosen differently - maybe for compatibility reasons?

MarkSil [MSFT] responded:

@Thomas: Thanks for raising that question. The WebBrowser Control determines the doc mode the same way that IE does because it contains the same web platform (e.g. there is one shared mshtml.dll across IE and WebBrowser Control hosts). The WebBrowser Control does default to the Compatibility View browser mode, which means that the default doc mode is IE7. Here is a blog post with more detail on this: blogs.msdn.com/.../more-ie8-extensibility-improvements.aspx.

To which Thomas responded:

@MarcSil (re: WebBrowser Control)

The problem with using registry entries to select document mode for WebControl is that it applies to the application as a whole. I write plugins for Google SketchUp where you have WebDialog windows to create UIs - it's just a WebBrowser control in a window. But that leads to problems as I want to force a document mode for my instance of the WebBrowser control, not for all of SU's WebBrowser controls as a whole.

So, my question is: how do you control the document mode per instance for a WebBrowser control?

Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125

3 Answers3

28

Have you tried setting in your html the

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

or

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

which means latest version

Gabriele Petrioli
  • 173,972
  • 30
  • 239
  • 291
  • 1
    Back, sorry. i tried adding `` and the document mode is still IE8. :( – Ian Boyd Nov 04 '10 at 20:32
  • 11
    This answer **does** work, but with one important note: The `X-UA-Compatible` directive must be placed in the `HEAD` *before* all other elements (except `TITLE` or other `META`). So it's easiest just to place it first. If it's placed, for example, after `STYLE`, then it will be ignored. – Ian Boyd Mar 21 '11 at 15:46
  • will edge render in IE8 if my browser is IE8? – climboid Mar 28 '12 at 20:13
  • 1
    @climboid, yes `IE=edge` means latest version supported by the installed IE browser. If yours is IE8 then it will render in IE8. ([keep in mind that this does not work for intranet sites](http://stackoverflow.com/a/6736646/128165)) – Gabriele Petrioli Mar 28 '12 at 20:15
  • This is not working for the WebBrowser control. Not sure what these guys have been puffing. Here's what the server sees on a page with that metadata on an IE11 browser, using ``Request.Browser``: ``Type = InternetExplorer11 Name = InternetExplorer Version = 11.0 Major Version = 11 Minor Version = 0`` Here's what I get from a WebControl, in the same page, from the same machine: ``Type = IE7 Name = IE Version = 7.0 Major Version = 7 Minor Version = 0`` – MoonStom Oct 07 '15 at 15:16
  • @MoonStom this answer is about setting the rendering mode (*assuming you are using the correct browser*). For your case have a look at https://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation and https://o2platform.wordpress.com/2012/04/17/set-net-webbrowser-control-to-use-latest-version-of-ie/ – Gabriele Petrioli Oct 07 '15 at 15:24
13

The IE9 "version" of the WebBrowser control, like the IE8 version, is actually several browsers in one. Unlike the IE8 version, you do have a little more control over the rendering mode inside the page by changing the doctype. Of course, to change the browser mode you have to set your registry like the earlier answer. Here is the location of FEATURE_BROWSER_EMULATION:

HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
     SOFTWARE
          Microsoft
               Internet Explorer
                    Main
                         FeatureControl
                              FEATURE_BROWSER_EMULATION
                                   contoso.exe = (DWORD) 000090000

Here is the complete set of codes:

  • 9999 (0x270F) - Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
  • 9000 (0x2328) - Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
  • 8888 (0x22B8) -Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
  • 8000 (0x1F40) - Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
  • 7000 (0x1B58) - Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

The full docs:

http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation

whitehawk
  • 2,449
  • 27
  • 31
  • 2
    That is as far as i got. But then the question arises, what if I set the value to 9999 but the client is IE8 (ie on XP where IE max version is 8)? Does it force the IE8 Standards mode or nothing at all? – Sgali Jun 10 '13 at 12:25
2

FEATURE_BROWSER_EMULATION does not works with CoInternetSetFeatureEnabled. The documentation of INTERNETFEATURELIST is not updated since IE7.

Since the feature setting is under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl you may be able to override the value in your process via a registry API hook.

Community
  • 1
  • 1
Sheng Jiang 蒋晟
  • 14,859
  • 2
  • 26
  • 44
  • i haven't gotten around to testing your idea. But upvoted because i never knew that there was such a feature as `GetOverrideKeyPath`. – Ian Boyd Nov 15 '10 at 19:32