在webkit(safari,chrome)中忽略的document.open(“text / plain”)格式设置 [英] document.open("text/plain") formatting ignored in webkit (safari, chrome)

查看:426
本文介绍了在webkit(safari,chrome)中忽略的document.open(“text / plain”)格式设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用document.open(text / plain)和document.write()从JavaScript创建页面。要呈现的文本是多行制表符分隔的文本。在Chrome 13.0.782.220 m和Safari 5.0.5中,它似乎以text / html格式呈现,忽略open()调用中的编码规范。通过说它似乎被呈现为HTML,我的意思是每个白色空间实例已被替换为一个空格字符。在Firefox 3.6,Opera 11.51和Internet Explorer 8中,这个呈现正确。这些结果在Windows和Mac OS / X上是一致的。

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.

我用[chrome] + text / plain,[safari] + text / plain和[ webkit] +文本/纯文本,并没有看到命中。我还在chrome和webkit bug报告网站中搜索了text / plain,并且没有看到类似的报告。

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.

是否有其他人跑过这个或有任何建议关于如何让我的数据在基于webkit的浏览器上正确显示?

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>


推荐答案

您需要在内容的周围使用< pre> 标签才能让它呈现您想要的样子。浏览器仍然会将您正在编写代码的文档视为HTML文档。 < pre> 标记会告诉浏览器将内容呈现为预格式化文本,该格式文本将兑现所有换行符和空格字符。

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.

这篇关于在webkit(safari,chrome)中忽略的document.open(“text / plain”)格式设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆