27

We have some legacy HTML content which we must render in compatibility mode. The requirement comes from our customers who want their HTML-based reports (some of which were created back in IE6 days) to look and print exactly the same, regardless of the browser version or underlying technologies. At the same time, we want to use Standard Mode and HTML5 for the rest of our web app.

An obvious solution is to host the legacy content in an <iframe> in compatibility mode. The following appears to work cross-browser:

main.html (in standard mode):

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title></title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 9pt;
            font-style: italic;
            font-weight: bold;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            info.firstChild.data = "document.compatMode: " + document.compatMode;
            // test frame's HTML5 API: document.getSelection()
            setInterval(function () {
                var selection = document.getElementById("contentFrame").contentDocument.getSelection();
                var selectedNode = selection.focusNode;
                if (selectedNode)
                    info2.firstChild.data = "Selected node: " + selectedNode.nodeName + ", offset: " + selection.focusOffset;
                else
                    info2.firstChild.data = "";
            }, 500);

        }
    </script>
</head>
<body>
    <h1>Standard Mode Page</h1>
    <span>body font</span>
    <table border="1">
        <tr>
            <td>Table font</td>
        </tr>
    </table>
    <span>body font</span>
    <pre id="info">&nbsp;</pre>
    <pre id="info2">&nbsp;</pre>
    <iframe id="contentFrame" style="width: 500px; height: 300px;" src="frame.html"></iframe>
</body>
</html>

frame.html (in compatibility mode):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">
<html>
<head>
    <title></title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 9pt;
            font-style: italic;
            font-weight: bold;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            info.firstChild.data = "document.compatMode: " + document.compatMode;
            editable.focus();
        }
    </script>
</head>
<body>
    <h1>Compatibility Mode Frame</h1>
    <span>body font</span>
    <table border="1">
        <tr>
            <td>Table font</td>
        </tr>
    </table>
    <span>body font</span>
    <pre id="info">&nbsp;</pre>
    <div id="editable" contentEditable="true" style="border: 1px dotted red">
        Editable
    </div>
</body>
</html>

Note the difference in rendering the table, using the same CSS:


Standard and compatibility mode mix

My question to experienced web developers: is this a supported scenario which can be used in production environment (IE8+ mostly, but occasionally Safari/Chrome/Firefox)? Is there a better way of doing this?

I've stumbled upon a related, albeit opposite question, which left me with mixed feelings.

[UPDATE] (based on the comments):

All JavaScript resides inside the main page and appears to work just fine. What's interesting (and great!), the inner frame's view is rendered in compatibility mode, yet standard mode features are available for its DOM (at least, when accessed from the main page). E.g. document.getSelection works (and does cross-browsers, too).

By supported scenario I mean any endorsement by W3C HTML and DOM standards. So far I haven't found a definitive answer to this. This behavior may as well be just a nice side effect, although the fact it works cross-browsers is promising.

MSDN says the following: As of IE9 mode, webpages cannot display multiple document modes. For example, consider a standards-based webpage that contains a frame element that displays content in quirks mode. IE9 mode displays the child frame in standards mode (because the parent document is in standards mode). According to my tests, this is not true; my sample works as desired in IE9: the main page is in standard mode, the frame page is in quirk mode. [EDITED] As pointed out in the comments, it is the Almost Standard Mode (i.e., not the classic quirk mode), with its own rendering rules.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • So can I confirm the following: that they wish for IE6-compatible reports to able to be viewed in exactly the same way in the latest version of Chrome/Firefox, as well as IE8+? – Qantas 94 Heavy Sep 30 '13 at 07:01
  • @Qantas94Heavy, correct. If not in exactly the same way, then as close as possible, without modifying the content. Most importantly, they want that for IE9/IE10 too. So far, the compatibility mode seems to be getting us there. – noseratio Sep 30 '13 at 09:26
  • 2
    I can't see why the combination should pose any trouble. Not sure how different versions of javascript environment will interact, however. – John Dvorak Sep 30 '13 at 11:52
  • 1
    @JanDvorak, all JavaScript resides in the top page and appears to work just fine. What's interesting (and great!), the inner frame's view is rendered in quirk mode, but standard mode features are still available for its DOM (at least, when accessing it from the main page). E.g. [document.getSelection](http://msdn.microsoft.com/en-us/library/ie/ff975169(v=vs.85).aspx) works (and does cross-browsers, too). – noseratio Sep 30 '13 at 12:58
  • 1
    Interesting - now you made me want to run a bunch of tests on my own. I might even report back here when I've got some conclusions. – John Dvorak Sep 30 '13 at 13:07
  • "is this a supported scenario" - supported by who? – robertc Sep 30 '13 at 13:23
  • @robertc: supported by *browser standards*, like W3C DOM. I was unable to find a definitive answer to that. It may as well be a side effect, although the fact it appears to work x-browsers is promising. – noseratio Sep 30 '13 at 13:26
  • Support from Microsoft is detailed here: http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx – Luke Sep 30 '13 at 15:47
  • @Luke, the info on that page isn't entirely correct (or up-to-date). It says: *As of IE9 mode, webpages cannot display multiple document modes. For example, consider a standards-based webpage that contains a frame element that displays content in quirks mode. IE9 mode displays the child frame in standards mode (because the parent document is in standards mode).* According to my tests, this is not true; **my sample works as desired in IE9**: the main page is in standard mode, the frame page is in quirk mode. – noseratio Sep 30 '13 at 16:53
  • 1
    Your inner page is presented in something called Almost Standards Mode, see here: http://msdn.microsoft.com/en-us/library/ff405912(v=vs.85).aspx . So you might get Standards Mode DOM and correct CSS behavior in your inner frame instead of quirks. – Daniel S. Oct 02 '13 at 05:37
  • @DanielS., thanks, I was not aware of Almost Standard Mode. So far, it does the job, but we'll keep watching. – noseratio Oct 02 '13 at 07:35
  • 1
    @Noseratio, I am glad that it works. I'm sad that the documentation provided for us to keep things working on IE are often not born out in testing. However, the inconsistencies between what they say it does and how it actually performs usually indicates something that will change in newer releases and shouldn't be relied upon. Best of luck, and thanks! – Luke Oct 04 '13 at 18:39

2 Answers2

8

As of IE9 mode, webpages cannot display multiple document modes. For example, consider a standards-based webpage that contains a frame element that displays content in quirks mode. IE9 mode displays the child frame in standards mode (because the parent document is in standards mode).

According to my tests, this is not true; my sample works as desired in IE9: the main page is in standard mode, the frame page is in quirk mode.

Not quite; when your sample works as desired it's actually displayed in a single display mode, with quirks mode emulated for the frame content. You shouldn't care about the underlying mechanics as long as the resulting output matches what you were after, although there is some anecdotal evidence of differences between emulated and native modes (mostly to do with js DOM manipulation).

I'd be a bit more concerned about how IE10+ would handle such fringe scenarios:

Starting with IE11 Preview, document modes are deprecated and should no longer be used, except on a temporary basis. Make sure to update sites that rely on legacy features and document modes to reflect modern standards.

Ninja edit: looks like this has already been resolved on SO; modifying the accepted solution to your needs you should omit Doctype and add <meta http-equiv="X-UA-Compatible" content="IE=5" />; X-UA-Compatibility correctly defined as per msdn spec

Community
  • 1
  • 1
Oleg
  • 24,465
  • 8
  • 61
  • 91
  • 2
    I'm upvoting this answer for the reasons I just commented in my answer. If your primary target is the internet then an HTML solution is preferable. I, personally, dislike having to dodge IE bugs with this sort of specific agility. But your tone implied that IE is a primary target. I would suggest gathering usage data to determine how much traffic would be impacted if you didn't correct the output for legacy IE versions. It might be small enough to not warrant the time investment. – Luke Oct 03 '13 at 19:13
2

This is a pseudo-answer to your slightly non-specific question;

In regards to your apprehension to rely on this IE feature for "back-compatibility", I feel the same way. Microsoft has provided this option because there are lots of companies out there who take a long time to update their web content. This option is meant to allow them to have a quick and dirty stop-gap, not a permanent solution.

So, what's the permanent solution? If that is your question, then IMO this is the answer; don't rely on the stop-gap and develop the correct output for the reports.

Without knowing what those reports are it is impossible to properly advise you on that part, but here's a stab in the dark:

There are lots of options to convert "HTML" to PDF. (I put HTML in quotations because each rendering engine is bound to require different versions/standards of HTML and you'll need to know those assumptions before you pick one.) If you want output that will format 100% the same on any browser, then you want a format that is meant to be static and not change; like PDF. Plus, then you also have the printing options taken care of as well.

Luke
  • 435
  • 4
  • 12
  • This answer assumes you have server-side scripting available. If you don't, then you really only have HTML to turn to and this answer is non-applicable. – Luke Sep 30 '13 at 15:28
  • This answer is useful. We're indeed looking at PDF rendering, but we haven't found yet a proper server-side tool that would provide 1:1 rendering from existing legacy quirk-mode HTML to PDF (having considered MSHTML and Awesomium so far). – noseratio Sep 30 '13 at 20:43
  • 1
    I'm not downvoting, but I've died a little inside when I've read the html-to-pdf suggestion. Might as well use silverlight `/s`, perhaps instead if updating the "in-frame" app template is practical it should be done? – Oleg Oct 02 '13 at 00:37
  • @o.v., could you please elaborate on why html-to-pdf is not feasible? – noseratio Oct 02 '13 at 15:16
  • 1
    HTML is not a web format. That is 100% true. It is a *print* document format (PDF). Using it to display reports, inasmuch as those reports are historic and not subject to change, is reasonable only if those reports are intended to be printed and/or saved. If the web is the primary consumption medium my suggestion loses relevance. – Luke Oct 03 '13 at 19:01
  • The point I was driving at is that IE tends to be such a moving target that the most reliable way to deal with it is to avoid any reliance on it. – Luke Oct 03 '13 at 19:07
  • Oops. Re-reading years later I noticed I said something very stupid! HTML is a web format. I think I meant PDF is not a web format. My apologies. – Luke Apr 06 '16 at 21:27