Why does 'string'[0] behave differently on ie8 + IIS7.5 than other browsers or local file?

旧城冷巷雨未停 提交于 2019-12-06 07:19:57

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3985605/why-does-string0-behave-differently-on-ie8-iis7-5-than-other-browsers-or-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!