使用 javascript 强制 IE9 进入兼容模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9930641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:16:35  来源:igfitidea点击:

Force IE9 into compatibility mode using javascript

javascriptinternet-explorer-9ie-compatibility-mode

提问by Serhiy

I have a frame that needs compatibility mode but parent frame seems to be setting it so the following tag inside the frame does nothing.

我有一个需要兼容模式的框架,但父框架似乎正在设置它,因此框架内的以下标记不执行任何操作。

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

Is there anyway to apply compat mode to only the frame, or have the frame apply compat mode to the parent frame.

有没有办法只将兼容模式应用于框架,或者让框架将兼容模式应用于父框架。

Was thinking if there is a javascript method to switch modes I could apply it to the parent frame from the child frame.

正在考虑是否有一种 javascript 方法可以切换模式,我可以将它从子框架应用到父框架。

采纳答案by Serhiy

So here's what I've gathered.

所以这是我收集的。

The only thing that seems to work is the meta tag way.

唯一似乎有效的是元标记方式。

However, it doesn't appear to work in frames as IE9 has disabled the ability of one frame to be of different type than another.

但是,它似乎在框架中不起作用,因为 IE9 禁用了一个框架与另一个框架具有不同类型的能力。

The meta tag also can't be preceded by script or link tags, only and other meta tags from what I've noticed, otherwise it fails as well.

元标记之前也不能有脚本或链接标记,只能和我注意到的其他元标记,否则它也会失败。

TEST CASE:

测试用例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Test test</title>
        <meta name="keywords" content="keywords, keyword, keyword phrase, etc.">
        <!--<link href="test.css" rel="stylesheet" type="text/css" />-->
        <!--<script type="text/javascript">alert('test');</script>-->
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    </head>
    <body>
        inline-block <div style="display:inline-block">test</div>
    </body>
</html>

回答by pplewa

From top of my head you can try to create meta tag and insert it in the parent frame head. I'm not sure though if that would force the emulation.

从我的头顶,您可以尝试创建元标记并将其插入父框架头。我不确定这是否会强制进行仿真。

var meta = document.createElement('meta');
meta.setAttribute('http-equiv', 'X-UA-Compatible');
meta.setAttribute('content', 'IE=EmulateIE7');

parent.document.getElementsByTagName('head')[0].appendChild(meta);