怎样把WebBrowser控件放到IE9成标准?

2023-03-15前端开发问题
5

本文介绍了怎样把WebBrowser控件放到IE9成标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用自动化(即 COM 自动化)在 Internet Explorer (9) 中显示一些 HTML:

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

Internet Explorer 出现,显示我的 html,开头为:

<!DOCTYPE html><HTML><头>...

<块引用>

注意: html5 标准模式选择加入文档类型 html

除非文档不是ie9标准模式;它处于 ie8 标准模式:


如果我先将 html 保存到我的电脑:

然后查看那个 html文档,IE进入标准模式:

我的问题是如何更新我的 SpawnIEWithSource(String html) 函数以使浏览器进入标准模式?

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


一个更冗长、更难理解或可读性更强的代码示例,这无助于进一步的问题可能是:

IWebBrowser2 即;CoCreateInstance(CLASS_InternetExplorer, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_WebBrowser2, ie);即.AddRef();ie.Navigate2(about:blank");IHtmlDocument 文档;dispDoc = ie.Document;dispDoc.AddRef();dispDoc.QueryInterface(IHTMLDocument2, doc);dispDoc.Release()doc.Write(html);doc.Close();doc.Release();ie.Visible = true;即.Release();


更新

评论者在 ieblog 条目中询问 使用浏览器模式与 Doc 测试网站模式:

<块引用>

当 HTML 内容在嵌入的 web 控件中时,我们能否描述如何确定文档模式?似乎是选择了不同的文档模式 - 可能是出于兼容性原因?

MarkSil [MSFT] 回应:

<块引用>

@Thomas:感谢您提出这个问题.WebBrowser Control 以与 IE 相同的方式确定文档模式,因为它包含相同的 Web 平台(例如,IE 和 WebBrowser Control 主机之间有一个共享的 mshtml.dll).WebBrowser 控件默认为兼容性视图浏览器模式,这意味着默认的文档模式是 IE7.这是一篇博客文章,其中包含更多详细信息:blogs.msdn.com/.../more-ie8-extensibility-improvements.aspx.

Thomas 回应:

<块引用>

@MarcSil(回复:WebBrowser 控件)

使用注册表项为 WebControl 选择文档模式的问题在于它适用于整个应用程序.我为 Google SketchUp 编写插件,您可以在其中使用 WebDialog 窗口来创建 UI——它只是一个窗口中的 WebBrowser 控件.但这会导致问题,因为我想为我的 WebBrowser 控件实例强制使用文档模式,而不是对整个 SU 的所有 WebBrowser 控件.

所以,我的问题是:如何控制 WebBrowser 控件的每个实例的文档模式?

解决方案

你试过在你的html中设置

表示最新版本

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:


If i save the html to my computer first:

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

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?

解决方案

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

这篇关于怎样把WebBrowser控件放到IE9成标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6

合并两个编译步骤的 source maps
Combine source maps of two compilation steps(合并两个编译步骤的 source maps)...
2024-04-20 前端开发问题
11