zoukankan      html  css  js  c++  java
  • textarea \n \r\n issue

    October 05, 2005

    How are newlines represented in an HTML textarea ?

    Writing about web page http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4

    OK, this is just a blatant attempt at stuffing a result into google, since it took me a while to find this out. I hope someone else finds it useful.

    If you have an HTML text area, and you want to parse the content of said textarea as a list of lines, you might be wondering what character you need to look for to indicate a line break. \n ? \r\n? That wierd thing that macOS 9 does?

    Contrary to what you might expect, the answer is not "it depends on the platform". The merciful W3C (blessed be their spec.) decreed it to be constant regardless of what hare-brained operating system choice your user has made.

    If your form's enctype is application/x-www-form-urlencoded (the default) , then the answer is '%0D%0A' i.e. CRLF, url-encoding. If the enctype is multipart/form-data, then it's an unencoded CRLF. This behaviour, it turns out, is inherited from the MIME specification.

    Since any binding library worth it's salt will decode the url-encoding for you, the string that you should look for is CR-LF. This is not the same as the default line separator (LF) on any unix box – if you use that, you'll end up with a spurious \r character at the end of each line of parsed text.


    - 3 comments by 1 or more people Not publicly viewable

    1. You beat me to it :) I wanted to blog this ;)

      11 Oct 2005, 13:25

    2.  

      Mike McNally

      An interesting problem here is that, what you note being true, the count of characters Javascript reports in your textarea may not be the same as the actual length received on the server when the form is submitted. Firefox (on Linux, at least, and I think also on Windows though I'm not sure) counts line separators as being one character long, because it apparently uses a lone linefeed character as the separator. The browser does, however, expand that linefeed to a CRLF pair when submitting the value.

      12 Oct 2005, 01:00

    3.  

      Larsson

      Thanks so much. I have been struggling with a post request and could not understand the lack of output. This was the answer I have sought

      25 Apr 2007, 20:23


    Add a comment

    You are not allowed to comment on this entry as it has restricted commenting permissions.

    Trackbacks

  • 相关阅读:
    编码和字符集
    【机器学习】模型泛化
    asp.net GridView控件的列属性
    asp.net截取指定长度的字符串内容
    asp.net 对数据库表增加,删除,编辑更新修改
    asp.net 链接数据库ADO.NET
    常用正则表达式 验证电子邮件网址邮政编码等
    ASP.NET获取文件的相关知识
    C#获取picturebox图片路径
    C# dataGridView根据数据调整列宽
  • 原文地址:https://www.cnblogs.com/lexus/p/1849069.html
Copyright © 2011-2022 走看看