开发者

document.open("text/plain") formatting ignored in webkit (safari, chrome)

I am creating a page from JavaScript using document.open("text/plain") and document.write(). The text to be rendered is multiple lines of tab separated text. In Chrome 13.0.782.220 m and Safari 5.0.5, it seems to be rendered as text/html, ignoring the encoding specification in the open() call. By saying that it seems to be rendered as HTML, I mean that each instance of white space has been replaced by a single space character. In Firefox 3.6, Opera 11.51 and Internet Explorer 8, this renders correctly. These results are consistent across Windows and Mac OS/X.

I've searched stackoverflow with [chrome]+text/plain, [safari]+text/plain and [webkit]+text/plain, and saw no hits. I've also searched for text/plain in the chrome and webkit bug reporting 开发者_运维百科sites, and seen no similar reports.

Has anyone else run across this, or have any suggestions on how to get my data to render correctly on webkit-based browsers?

A simple demo page for this problem is:

<html><head>
<script type="text/javascript">
    function displayInNewWindow() {
        var win;
        var doc;
        win = window.open("", "WINDOWID");
        doc = win.document;
        doc.open("text/plain");
        doc.write("Line1\tField2\tField3\nLine2\tField2\tField3\n");
        doc.close();
    }
</script>
</head>
<body>
    <script type="text/javascript">displayInNewWindow();</script>
    <p>We're here!</p>
</body>


You should drop the parameter to open() since it's not supported in other browsers, and prefix your parameter to the first write() with '<PLAINTEXT>', a beyond-deprecated tag from the depths of HTML history which does exactly what you want (it has no closing tag.)


You need to use the <pre> tag around your content in order to get it to render the way you want it to. The browser is still going to treat the document you're writing the code into as an HTML document. The <pre> tag will tell the browser to render the content a pre-formatted text, which will honor all line breaks and white-space characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜