document.open(“text/plain”) 格式在 webkit 中被忽略(safari、chrome)

发布于 2024-12-03 05:03:25 字数 1066 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2024-12-10 05:03:25

您应该将参数放到 open() 中,因为其他浏览器不支持它,并在第一个 write() 参数中添加 '

<code>'&lt;/code&gt; 前缀,这是一个来自深处的已过时的标记HTML 历史记录,它完全符合您的要求(它没有结束标记。)&lt;/plaintext&gt;&lt;/code&gt;&lt;/p&gt;</code></plaintext></div><div class="wt-content wt-comment-content hide pptcOriginalCommentContent_22635448"><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 <code>'&lt;PLAINTEXT&gt;'</code>, a beyond-deprecated tag from the depths of HTML history which does exactly what you want (it has no closing tag.)</p></div><div class="wt-comments-tools"><span class="wt-info-model"><a href="javascript:" class="reply_btn" data-post_id="7707823" data-post_type="topic" data-post_author="3950" data-comment_id="22635448" data-comment_author="赤濁"><i class="wjsp wjsp-huifu"></i> 回复 </a></span><span class="wt-info-model"><a href="javascript:" data-id="22635448" data-type="comment" data-count="" class="follow_btn"><i class="wjsp wjsp-wujiaoxing"></i> 收藏 0 </a></span><a class="wt-info-model topicOriginalBtn" data-long_text="1" data-content_class="pptcOriginalCommentContent_22635448" href="javascript:">原文 <i class="wjsp wjsp-xiangyou"></i></a></div></div></div><div class="wt-comments-item" id="comment-22635449"><div class="pull-left wt-avatar"><a href="https://www.wenjiangs.com/author/udocywyl" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/uploads/avatar/03111416999027.jpg" width="96" height="96"></a></div><div class="wt-comments-text"><div class="wt-comments-info"><span class="wt-info-model"><a href="https://www.wenjiangs.com/author/udocywyl" rel="nofollow">风蛊</a> </span><span class="wt-info-model">2024-12-10 05:03:25</span></div><div class="wt-content wt-comment-content"><p>您需要在内容周围使用 <code></code></p><pre><code></code> 标记,以便使其按照您想要的方式呈现。浏览器仍然会将您编写代码的文档视为 HTML 文档。 <code></code><pre><code></code> 标签将告诉浏览器将内容呈现为预先格式化的文本,这将尊重所有换行符和空白字符。</pre> <p></p> </pre></div><div class="wt-content wt-comment-content hide pptcOriginalCommentContent_22635449"><p>You need to use the <code>&lt;pre&gt;</code> 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 <code>&lt;pre&gt;</code> tag will tell the browser to render the content a pre-formatted text, which will honor all line breaks and white-space characters.</p></div><div class="wt-comments-tools"><span class="wt-info-model"><a href="javascript:" class="reply_btn" data-post_id="7707823" data-post_type="topic" data-post_author="3950" data-comment_id="22635449" data-comment_author="风蛊"><i class="wjsp wjsp-huifu"></i> 回复 </a></span><span class="wt-info-model"><a href="javascript:" data-id="22635449" data-type="comment" data-count="" class="follow_btn"><i class="wjsp wjsp-wujiaoxing"></i> 收藏 0 </a></span><a class="wt-info-model topicOriginalBtn" data-long_text="1" data-content_class="pptcOriginalCommentContent_22635449" href="javascript:">原文 <i class="wjsp wjsp-xiangyou"></i></a></div></div></div><div class="loadMore">~没有更多了~</div></div></div></div><div class="modal" tabindex="-1" role="dialog" id="emailSubscribe"><div class="modal-dialog" role="document"><div class="esciBox"><h2>绑定邮箱获取回复消息</h2><p>由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!</p><div class="esciInput"><input type="email" class="form-control" v-model="user_email" placeholder="请输入您的邮箱"></div><div class="esciBtn"><button type="button" class="btn btn-primary" @click="bindEmail2">确认绑定</button></div></div></div></div></div></div><div class="col-md-4"><div class="side-topic-author"><div class="wt-container"><div class="mod-tit"><h3>关于作者</h3></div><div class="side-single-author-avatar"><a class="pull-left side-single-author-avatar-img" href="https://www.wenjiangs.com/author/efzsq8hl" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/uploads/avatar/q101802080932898.jpg" width="96" height="96"> </a><a class="side-single-author-avatar-txt" href="https://www.wenjiangs.com/author/efzsq8hl" rel="nofollow">痞味浪人</a><p>暂无简介</p></div><div class="author-tatol side-single-author-count"><div class="author-total-item"><span class="item-num">0</span> <span class="item-name">文章</span></div><div class="author-total-item"><span class="item-num">0</span> <span class="item-name">评论</span></div><div class="author-total-item"><span class="item-num">23</span> <span class="item-name">人气</span></div></div><div class="side-single-author-do"><a href="javascript:" data-id="3950" data-type="user" class="follow_btn btn btn-success"><i class="wjsp wjsp-jia2"></i> 关注 </a><a data-href="/message?accept=3950" target="_blank" class="btn btn-info"><i class="wjsp wjsp-xiaoxi"></i> 发私信</a></div></div></div><div class="side-group"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6502133150423173" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display: block" data-ad-client="ca-pub-6502133150423173" data-ad-slot="3893261597" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div><div class="side-img"><a href="/downclient" title="客户端下载"><img src="/public/img/sidebar.png"></a></div><div class="side-topic-author"><div class="wt-container"><div class="mod-tit"><h3>相关话题</h3></div><div class="mod-con"><ul class="side-topic-rand-list"><li><a href="https://www.wenjiangs.com/group/topic-6432575.html" title="自动 Word 邮件合并未按预期工作">自动 Word 邮件合并未按预期工作</a></li><li><a href="https://www.wenjiangs.com/group/topic-6576771.html" title="关于从使用块返回的最佳实践">关于从使用块返回的最佳实践</a></li><li><a href="https://www.wenjiangs.com/group/topic-6579331.html" title="我如何获得图表系列? 父母的父母的详细信息?">我如何获得图表系列? 父母的父母的详细信息?</a></li><li><a href="https://www.wenjiangs.com/group/topic-6417602.html" title="如何设置树结构子节点的顺序">如何设置树结构子节点的顺序</a></li><li><a href="https://www.wenjiangs.com/group/topic-7226308.html" title="按大小排序地图">按大小排序地图</a></li><li><a href="https://www.wenjiangs.com/group/topic-6531736.html" title="在 MSAccess 中,在 nvarchar 中插入 NULL 失败">在 MSAccess 中,在 nvarchar 中插入 NULL 失败</a></li><li><a href="https://www.wenjiangs.com/group/topic-6432573.html" title="无法加载类型... 升级到 W2K3 和 IIS6 时">无法加载类型... 升级到 W2K3 和 IIS6 时</a></li><li><a href="https://www.wenjiangs.com/group/topic-6466490.html" title="最好的分布式暴力对抗措施是什么?">最好的分布式暴力对抗措施是什么?</a></li><li><a href="https://www.wenjiangs.com/group/topic-6569481.html" title="MVC 使用 Linq to Entity 和 sql 加密">MVC 使用 Linq to Entity 和 sql 加密</a></li><li><a href="https://www.wenjiangs.com/group/topic-6569482.html" title="C# 的高效缩放">C# 的高效缩放</a></li></ul></div></div></div><div class="side-tags"><div class="wt-container"><div class="mod-tit"><a href="/tags" class="pull-right">更多 <i class="wjsp wjsp-xiangyou"></i></a><h3>热门标签</h3></div><div class="mod-con"><a href="https://www.wenjiangs.com/tag/operating-system-xht">操作系统</a> <a href="https://www.wenjiangs.com/tag/programming-oci">程序设计</a> <a href="https://www.wenjiangs.com/tag/it-operation-and-maintenance">IT运维</a> <a href="https://www.wenjiangs.com/tag/linux%e7%b3%bb%e7%bb%9f%e7%ae%a1%e7%90%86">Linux系统管理</a> <a href="https://www.wenjiangs.com/tag/javascript">JavaScript</a> <a href="https://www.wenjiangs.com/tag/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ba%94%e7%94%a8">服务器应用</a> <a href="https://www.wenjiangs.com/tag/solaris">solaris</a> <a href="https://www.wenjiangs.com/tag/c-c">C/C++</a> <a href="https://www.wenjiangs.com/tag/php">PHP</a> <a href="https://www.wenjiangs.com/tag/shell">Shell</a> <a href="https://www.wenjiangs.com/tag/bsd">BSD</a> <a href="https://www.wenjiangs.com/tag/vue-js">Vue.js</a> <a href="https://www.wenjiangs.com/tag/aix">aix</a> <a href="https://www.wenjiangs.com/tag/oracle">Oracle</a> <a href="https://www.wenjiangs.com/tag/python">Python</a> <a href="https://www.wenjiangs.com/tag/html">HTML</a> <a href="https://www.wenjiangs.com/tag/%e7%b3%bb%e7%bb%9f%e7%ae%a1%e7%90%86">系统管理</a> <a href="https://www.wenjiangs.com/tag/html5">HTML5</a> <a href="https://www.wenjiangs.com/tag/css">CSS</a> <a href="https://www.wenjiangs.com/tag/%e5%89%8d%e7%ab%af">前端</a></div></div></div><div class="side-group site-recUser"><div class="wt-container"><div class="mod-tit"><a href="/people" class="pull-right">更多 <i class="wjsp wjsp-xiangyou"></i></a><h3>推荐作者</h3></div><div class="mod-con"><div class="row wt-group"><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/tt_fWOfqD27" rel="nofollow"><img src="https://www.wenjiangs.com/public/img/avatars.png" width="96" height="96"></a></div><a data-id="707" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/tt_fWOfqD27" rel="nofollow">已经忘了多久</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/15867725375" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/themes/wtheme/images/avatars.png" width="96" height="96"></a></div><a data-id="752" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/15867725375" rel="nofollow">15867725375</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/LonelySnow" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/themes/wtheme/images/avatars.png" width="96" height="96"></a></div><a data-id="820" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/LonelySnow" rel="nofollow">LonelySnow</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/9drvl10i" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/uploads/avatar/q101801285026523.jpg" width="96" height="96"></a></div><a data-id="3779" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/9drvl10i" rel="nofollow">走过海棠暮</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/fp4gfc8" rel="nofollow"><img src="https://www.wenjiangs.com/wp-content/uploads/avatar/q101802432338068.jpg" width="96" height="96"></a></div><a data-id="4140" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/fp4gfc8" rel="nofollow">轻许诺言</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div><div class="col-md-12"><div class="pull-left wt-group-avatar"><a href="https://www.wenjiangs.com/author/wxsp_4aU0cSk8" rel="nofollow"><img src="https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eo2nk9s8GOOfwbp26fyQO2TsWV3s1Y14AlwERKhgH9hdbG2huRia9RXYd9LCfE3YC0snglle1ImLyQ/132" width="96" height="96"></a></div><a data-id="5223" data-type="user" href="javascript:" class="follow_btn pull-right">关注</a><div class="wt-group-text"><h3 class="wt-group-tit"><a href="https://www.wenjiangs.com/author/wxsp_4aU0cSk8" rel="nofollow">信馬由缰</a></h3><p><span class="wt-info-model">文章 0</span> <span class="wt-info-model">评论 0</span></p></div></div></div></div></div></div><div class="side-friendship"><div class="wt-container"><div class="mod-tit"><a href="/friendship" class="pull-right">更多 <i class="wjsp wjsp-xiangyou"></i></a><h3>友情链接</h3></div><div class="mod-con"><a href="http://www.wenjiangs.com" target="_blank">文江博客</a></div></div></div><div class="side-toc"><div class="wt-container"><div class="mod-con"><div class="singleToc"><ul></ul></div></div></div></div></div></div></div></div><footer class=""><ul class="flink"><li><a href="/donation">捐赠本站</a></li><li><a href="/friendship">友情链接</a></li><li><a href="/specification">使用规范</a></li><li><a href="/agreement">服务协议</a></li><li><a href="/about">关于我</a></li></ul><p>©文江博客 Wenjiangs.com 2017-2024 / <a href="http://beian.miit.gov.cn" target="blank">蜀ICP备13016540号-2</a></p></footer><div class="side-tool"><div><a href="https://www.wenjiangs.com/feedback"><i class="wjsp wjsp-fankuiyijianfankui-xianxing"></i></a> <a href="javascript:" class="backup"><i class="wjsp wjsp-xiangshang"></i></a> <a href="javascript:" class="toComment"><i class="wjsp wjsp-liaotian2"></i></a> <a href="javascript:" class="toBottom"><i class="wjsp wjsp-xiangxia"></i></a></div></div><div class="privacy"><div class="privacyLeft">我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 <a href="/agreement">隐私政策</a> 了解更多相关信息。 单击 <code>接受</code> 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。</div><div class="privacyBtn"><button class="btn btn-default">取消</button> <button class="btn btn-primary">接受</button></div></div><div class="originaltextBox"><div class="otbTitle"><span class="otbtText">原文</span> <span class="otbtClose" aria-hidden="true">×</span></div><div class="otbContent"><div class="wt-content"></div></div></div></body></html>