如果你只能控制 body 的内容,如何强制 IE 以标准模式渲染?
我有一个特殊的情况,我只能控制文档的内容 。我认为,为了保持灵活性,主机没有声明一个文档类型,该文档类型将立即使 IE 进入怪异模式。由于我对文档的控制有限,我该如何强制 IE 以标准模式呈现页面?
I have a peculiar situation where I am only given control of the contents of a document's <body>. The host, I assume in an effort to remain flexible, is not declaring a doctype which will throw IE into quirks mode immediately. With my limited control over the document, what can I do to force IE to render the page in standards mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信你对此无能为力,除非你说,用JS动态重写页面内容并强制插入doctype。
您能否详细说明一下您对
有多少控制权?你可以做 JS/脚本吗?
编辑:这是一个尝试,但我没有在 IE 中测试它。它可能会给你一些想法。我
document.write()
document.documentElement
的outerHTML
,它将 compatMode 转换为 CSS1Compat。您可能需要在重写时删除脚本块。就像我说的,我真的不建议尝试这个...
http: //medero.org/first-line.html
编辑#2:它似乎在 IE6 中出奇地有效。但刷新后,IE 会以某种方式缓存它,并永久保留其
.document.write()
形式。为了解决这个问题,请在其后面附加一个查询字符串,例如?203984234
。再说一次,我不确定你的情况是什么,但我希望这能给你带来想法或帮助。
编辑#3:我重写了它并将
document.write
绑定到window.onload
。每次访问它时,您都需要附加一个唯一的查询字符串才能看到效果,因为它会在.write
之后缓存它。http://medero.org/rewrite.html?f30324433322111
如果您需要更即时的东西,也许可以利用 jQuery 的 DOM 就绪函数在窗口加载之前重写它。
其他注释:
document.write
(如果视觉上重要的话)document.write在保存
outerHTML
之前,这样新编写的页面就没有script
块。I believe you can't do anything about it unless you say, rewrite the contents of the page dynamically with JS and forcefully insert a doctype.
Can you go into specifics of how much control you have over the
<body>
? Are you allowed to do JS/scripting?EDIT: Here's an attempt but I didn't test it in IE. It may give you ideas. I
document.write()
theouterHTML
ofdocument.documentElement
and it turns the compatMode into CSS1Compat.You may need to strip out the script block upon rewrite. Like I said, I wouldn't really recommend trying this...
http://medero.org/first-line.html
EDIT #2: It seems to surprisingly work in IE6. But upon refresh, IE caches it somehow and it permanently stays in its
.document.write()
ed form. To counter that, append it with a query string, eg?203984234
.Again, I'm not sure what your situation is, but I hope this gives you ideas or helps.
EDIT #3: I rewrote it and bound the
document.write
towindow.onload
. You will need to append a unique query string every time you visit it to see the effect, because it caches it after it.write
's it.http://medero.org/rewrite.html?f30324433322111
If you need something more instantaneous you can probably jack jQuery's DOM ready function to rewrite it before the window loads.
Miscellaneous Notes:
document.write
is invoked if visually it matters<script>
document.write before savingouterHTML
so that the newly written page doesn't have thescript
block.查看这篇定义文档兼容性文章在 MSDN 上。也许写出
X-UA-Compatible
元标记会起作用。Have a look at this Defining Document Compatibility article on MSDN. Perhaps writing out the
X-UA-Compatible
meta tag will work.这是我设法解决它的方法,基于 meder 的回答。
我首先尝试编写
X-UA-Compatible
元数据,但没有成功。在文档的末尾(或在头部,但我也无法访问它):
内部
if
是为了防止无限循环。最好去掉代码本身,比如将其放入函数中并使用string.replace()
删除函数调用,但我无法让它工作,所以不得不解决为了这。Here's how I managed to solve it, based on meder's answer.
I first tried writing out the
X-UA-Compatible
meta but that didn't work.At the end of the document (or in the head, but I didn't have access to that either):
The inner
if
is to prevent an infinite loop. It would probably better to strip out the code itself, say by putting it in a function and removing the function call withstring.replace()
, but I couldn't get that to work so had to settle for this.