为什么 'string'[0] 在 ie8 上表现不同 + IIS7.5比其他浏览器还是本地文件好?

发布于 2024-09-28 13:59:45 字数 847 浏览 1 评论 0原文

考虑以下 Html 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>test</title>
        <style type="text/css">

        </style>
    </head>
    <body>
        <script type="text/javascript">
            alert('' +
                '\'test\'[0] = \'' + 'test'[0] + '\'\n' +
                '\'test\'.charAt(0) = \'' + 'test'.charAt(0) + '\'\n'
            );
        </script>
    </body>
</html>

当我在本地计算机上打开此文件时,它会给出以下输出(在 ie8 和 Chrome 中):

'test'[0] = 't'
'test'.charAt(0) = 't'

当我在 IIS7.5 上托管此文件时,我仍然在 Chrome 中得到相同的结果,但 ie8 给出以下输出结果:

'test'[0] = 'undefined'
'test'.charAt(0) = 't'

这怎么可能?

Consider the following Html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>test</title>
        <style type="text/css">

        </style>
    </head>
    <body>
        <script type="text/javascript">
            alert('' +
                '\'test\'[0] = \'' + 'test'[0] + '\'\n' +
                '\'test\'.charAt(0) = \'' + 'test'.charAt(0) + '\'\n'
            );
        </script>
    </body>
</html>

When I open this file on my local machine it gives the following output (both in ie8 and Chrome):

'test'[0] = 't'
'test'.charAt(0) = 't'

When I host this file on IIS7.5 I still get the same result in Chrome but ie8 gives the following result:

'test'[0] = 'undefined'
'test'.charAt(0) = 't'

How is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

扮仙女 2024-10-05 13:59:45

Jan,只是为了添加您自己的答案,启用兼容性视图的更符合标准的方法是在文档的开头使用标准文档类型。

<!DOCTYPE html>

这是 HTML5 文档类型,也应该让您进入标准模式。

Jan, just to add to your own answer, the more standards compliant way to enable compatibility view would be to just use a standard doctype at the beginning of your document.

<!DOCTYPE html>

Which is the HTML5 doctype, should put you into standards mode as well.

心清如水 2024-10-05 13:59:45

好吧,我发现这是由于兼容性视图造成的。 Internet Explorer 是标准配置(我想,我不是这台笔记本电脑的所有者),可以选择在兼容性视图中显示所有 Intranet 页面。为了强制它使用 ie8 兼容性视图,我必须将以下元标记添加到 head 元素:

<meta http-equiv="X-UA-Compatible" content="IE=edge" > <!-- IE8 mode -->

您还可以在“页面”>“页面”下关闭此选项。 “兼容性视图设置”

此问题有让我朝着正确的方向前进。

编辑:我建议网络开发人员在 ie8 中保留此选项,因为大多数用户都会选中它。这样您就可以在开发周期中捕获这些错误。

编辑2: sv_in 是对的。 IE=edge 更加简洁。

OK I found out it was due to compatibility view. Internet explorer comes standard (I think, I am not the owner of this laptop) with the option to display all intranet pages in compatibility View. To force it to use ie8 compatibility view I had to add the following meta tag to the head element:

<meta http-equiv="X-UA-Compatible" content="IE=edge" > <!-- IE8 mode -->

You can also turn this option off under 'Page' > 'Compatibility View Settings'

This question has put me in the right direction.

EDIT: I advise web developers to leave this option checked in ie8, because most of your users will have it checked. This way you will catch these errors in the development cycle.

EDIT2: sv_in is right. IE=edge is much cleaner.

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