Html 浏览器是发送“\r\n”还是“\n”还是取决于浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6324167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:04:33  来源:igfitidea点击:

Do browsers send "\r\n" or "\n" or does it depend on the browser?

htmlbrowserweb-standardsline-breaks

提问by Timothy Khouri

This question has bothered me for a million years... whenever I create a website with a textarea that allows multi-line (such as a "Bio" for a user's profile) I always end up writing the following paranoid code:

这个问题困扰了我一百万年......每当我创建一个带有允许多行(例如用户配置文件的“Bio”)的文本区域的网站时,我总是最终编写以下偏执代码:

// C# code sample...
bio = bio.Replace("\r\n", "\n").Replace("\r", "\n");
bio = Regex.Replace(@"\n{2,}", "\n\n");

So, what do browsers send up for a <textarea name="Bio"></textarea>if it has multiple lines?

那么,<textarea name="Bio"></textarea>如果a有多行,浏览器会发送什么?

采纳答案by Ted Hopp

The HTTPand MIMEspecs specify that header lines must end with \r\n, but they aren't clear (some would argue that it isn't clear if they are clear) about what to do with the contents of a TEXTAREA. (See, for instance, this threadfrom an HTML working group about the issue.)

HTTPMIME规范指定标题行必须以\ r \ n结束,但他们并不清楚(有些人会认为,如果他们都清楚,目前还不清楚)有关如何与textarea的内容做。(例如,请参阅来自 HTML 工作组的有关此问题的线程。)

Here's a quote from the HTTP/1.1 spec about message headers:

这是 HTTP/1.1 规范中关于消息头的引用:

The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR.

消息头字段的行终止符是序列 CRLF。但是,我们建议应用程序在解析此类标头时,将单个 LF 识别为行终止符并忽略前导 CR。

I think that is a good strategy in general: be strict about what you produce but liberal in what you accept. You should assume that you will receive all sorts of line terminators. (Note that in addition to CRLF and LF, Mac OS-9 used CR alone, and there are still a few of those around. The Unicode standard(section 5.8) specifies a wide range of character sequences that should be recognized as line terminators; there's a list of them here.)

我认为总的来说这是一个很好的策略:对你生产的东西严格,但在你接受的东西上放宽。您应该假设您将收到各种线路终止符。(注意,除了 CRLF 和 LF 之外,Mac OS-9 单独使用了 CR,并且周围还有一些。Unicode 标准(第 5.8 节)指定了应识别为行终止符的广泛字符序列;有它们的列表在这里。)

回答by bobince

what do browsers send up for a <textarea></textarea>if it has multiple lines?

<textarea></textarea>如果a有多行,浏览器会发送什么?

All modern browsers send CRLF (\r\n). However this is not something that has been satisfactorily standardised so I would definitely consider it worthwhile to normalise the newlines of all multi-line input text.

所有现代浏览器都发送 CRLF ( \r\n)。然而,这并不是一个令人满意的标准化的东西,所以我肯定认为对所有多行输入文本的换行符进行规范化是值得的。

When the value is read through JavaScript rather than being submitted directly from a form, browser behaviour differs. IE and Opera return strings with CRLFs in; Firefox and WebKit return LF. So any form that gets submitted with JavaScript/XMLHttpRequest help is likely to come in either form.

当值通过 JavaScript 读取而不是直接从表单提交时,浏览器行为会有所不同。IE 和 Opera 返回带有 CRLF 的字符串;Firefox 和 WebKit 返回 LF。因此,任何通过 JavaScript/XMLHttpRequest 帮助提交的表单都可能采用任何一种形式。