Search code examples
javahtmlservletsinternet-explorer-9weblogic

Internet explorer 9 will not recognize doctype sent by servlet from weblogic server and renders in document mode IE7 - extra characters before start?


PROBLEM

Internet Explorer 9 does not take the <!DOCTYPE html> into account in the same way when I have my page locally or on my server. I made a simple test page to highlight the problem:

EDIT: updated simple test page with meta tag and css link

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="css/environmentinfo.css" />
        <meta http-equiv="x-ua-compatible" content="IE=edge"/>
    </head>
    <body>
        <script>
            document.write(document.compatMode);
            document.write('<br/>');
            document.write(document.documentMode);
        </script>
    </body>
</html>

When I open the file on my local system in IE9 it shows:

CSS1Compat

9

When I deploy it on the server it shows:

CSS1Compat

7

SERVER

So I'm serving the file from a weblogic server. It is deployed in a simple war archive and it is served by the default servlet. The local file is just served by the filesystem (C:...\test.html in the url bar).

Debug info

I've looked at encoding (both the same) and the effective characters present (via notepad++ and via wireshark).

The local file:

<!DOCTYPE html>\r\n
<html lang="en">\r\n
    <head>\r\n
    </head>\r\n
    <body>\r\n
        <script>\r\n
            document.write(document.compatMode);\r\n
            document.write('<br/>');\r\n
            document.write(document.documentMode);\r\n
        </script>\r\n
    </body>\r\n
</html>

Server response:

HTTP/1.1 200 OK\r\n
Date: Fri, 13 Mar 2015 12:34:26 GTM\r\n
Accept-Ranges: bytes\r\n
Content-Length: 427\r\n
Content-Type: text/html\r\n
Last-Modified: Fri, 13 Mar 2015 12:29:30 GMT\r\n
X-Powered-By: Servlet/3.0 JSP/2.2\r\n
\r\n
<!DOCTYPE html>\r\n
<html lang="en">\r\n
    <head>\r\n
    </head>\r\n
    <body>\r\n
        <script>\r\n
            document.write(document.compatMode);\r\n
            document.write('<br/>');\r\n
            document.write(document.documentMode);\r\n
        </script>\r\n
    </body>\r\n
</html>

I've looked at some other similar questions and I've found that IE will ignore the <!DOCTYPE html> if there are any characters before it. I checked the html spec and it states headers, then a newline and then the start of the document. So it seems there are no extra characters. With wireshark, I could see the actual bytes and there don't seem to be any extra characters. Answers, tips and even other things to look at would all be appreciated.


Solution

  • IE may have some configuration settings that override <!doctype html> in the markup. As per https://msdn.microsoft.com/en-us/library/ie/jj676914(v=vs.85).aspx:

    Pages opened in the Intranet zone might be treated differently, depending on the configuration of the browser, the presence of group policy options, and other factors.

    Try to check places listed in the article above.

    In addition to that, you can try to force document compatibility mode using either X-UA-Compatible HTTP header or <meta> tag as per https://msdn.microsoft.com/en-us/library/ie/jj676913(v=vs.85).aspx

    In some nasty cases when a standards compliant page is loaded inside an iframe hosted by a quirks page it may be required to reload the iframe to force it to the right document mode.

    Please note that once IE starts rendering the page the document mode is finalized and can't be changed on the fly. Thus, every instruction on the document mode has to appear as early as it can. For example, as you already mentioned, <!doctype html> has to be the very 1st line, <meta> with X-UA-Compatible has to be the very 1st tag in the <head>. I'd say if you have control over the server side use HTTP header instead of <meta> to make sure it switches document mode before any rendering happens.