如果你只能控制 body 的内容,如何强制 IE 以标准模式渲染?

发布于 2024-09-12 18:09:26 字数 106 浏览 8 评论 0原文

我有一个特殊的情况,我只能控制文档的内容 。我认为,为了保持灵活性,主机没有声明一个文档类型,该文档类型将立即使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-09-19 18:09:26

我相信你对此无能为力,除非你说,用JS动态重写页面内容并强制插入doctype。

您能否详细说明一下您对 有多少控制权?你可以做 JS/脚本吗?

编辑:这是一个尝试,但我没有在 IE 中测试它。它可能会给你一些想法。我 document.write() document.documentElementouterHTML ,它将 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 就绪函数在窗口加载之前重写它。

其他注释:

  • 您可能可以通过 CSS 隐藏整个 html 文档,直到调用 document.write(如果视觉上重要的话)
  • 您可能应该删除

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() the outerHTML of document.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 to window.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:

  • You could probably hide the entire html document through CSS until the document.write is invoked if visually it matters
  • You should probably strip the <script> document.write before saving outerHTML so that the newly written page doesn't have the script block.
情栀口红 2024-09-19 18:09:26

查看这篇定义文档兼容性文章在 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.

囍孤女 2024-09-19 18:09:26

这是我设法解决它的方法,基于 meder 的回答

我首先尝试编写 X-UA-Compatible 元数据,但没有成功。

在文档的末尾(或在头部,但我也无法访问它):

<script type="text/javascript">
if (navigator.appName == 'Microsoft Internet Explorer'){
    window.onload=function(){
        if (document.documentMode == 5){
            contents = document.documentElement.outerHTML;
            newdoc = document.open("text/html", "replace");
            newdoc.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'); 
            newdoc.write(contents);
            newdoc.close();
        }
    };
}
</script>

内部 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):

<script type="text/javascript">
if (navigator.appName == 'Microsoft Internet Explorer'){
    window.onload=function(){
        if (document.documentMode == 5){
            contents = document.documentElement.outerHTML;
            newdoc = document.open("text/html", "replace");
            newdoc.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'); 
            newdoc.write(contents);
            newdoc.close();
        }
    };
}
</script>

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 with string.replace(), but I couldn't get that to work so had to settle for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文