document.open(“text/plain”) 格式在 webkit 中被忽略(safari、chrome)
我正在使用 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 中是一致的。
我用 [chrome]+text/plain、[safari]+text/plain 和 [webkit]+text/plain 搜索了 stackoverflow,但没有看到任何结果。我还在 chrome 和 webkit 错误报告网站中搜索了 text/plain,但没有看到类似的报告。
有没有其他人遇到过这个问题,或者对如何让我的数据在基于 webkit 的浏览器上正确呈现有任何建议?
这个问题的一个简单的演示页面是:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将参数放到 open() 中,因为其他浏览器不支持它,并在第一个 write() 参数中添加
'
'</code> 前缀,这是一个来自深处的已过时的标记HTML 历史记录,它完全符合您的要求(它没有结束标记。)</plaintext></code></p>
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.