Strange line breaks

Source: Internet
Author: User

In unix-like, it is \ n (0x0A ). I thought there was no big problem. I did not expect that this time I was developing a small program, but it gave me a big headache.

First, we found this problem:

My program uses Extjs + php to communicate through ajax. There is a page,

Two text boxes, one text box and one textarea. When textarea contains carriage return and line feed, a script error occurs. It is found later that if the string returned by the server is wrapped in a line break, js parsing errors may occur. It can be seen that the syntax of js when parsing json data is consistent with the syntax of the locally defined string variable. A string cannot contain multiple lines. When a carriage return is used to wrap a line, the escape character \ n (\ r \ n) is required ).
However, this is not a problem I want to explain. The problem I want to explain is that when I solve this problem and capture packets for analysis, I found an interesting phenomenon, the encoding of the line breaks sent by the browser to the server is inconsistent.
For example, the string inCopy codeThe Code is as follows: Ext. Ajax. request ({
Url: 'catman. php ',
Form: this. FORM_NAME,
Scope: this,
Callback: function (o, s, r ){
Var retObj = Ext. decode (r. responseText );
If (retObj. success ){
Alert ('saved successfully! ');
}
}
});

The encoding sent to the server is as follows:
Catdesc = % E6 % 9E % 97% 0A % 0A % E5 % B0 % 910A % 0A % 0Aa % E5 % 8D % 8E
Note that % 0A marked in red is encoded as \ n. \ R code is missing.
Then, if I use form submit for direct submission, I find that the encoding is:
% E6 % 9E % 97% 0D % 0A % 0D % 0A % E5 % B0 % 91% 0D % 0A % 0D % 0A % 0D % 0D % 0Aa % E5 % 8D % 8E
\ R (% 0D) encoding is found.
(The above is the test result in IE9 .)
Is it ExtJs?
If you analyze the source code of Extjs, find the form value. As follows:Copy codeThe Code is as follows: serializeForm: function (form ){
Debugger
Var fElements = form. elements | (document. forms [form] | Ext. getDom (form). elements,
HasSubmit = false,
Encoder = encodeURIComponent,
Name,
Data = '',
Type,
HasValue;
Ext. each (fElements, function (element ){
Name = element. name;
Type = element. type;
If (! Element. disabled & name ){
If (/select-(one | multiple)/I. test (type )){
Ext. each (element. options, function (opt ){
If (opt. selected ){
HasValue = opt. hasAttribute? Opt. hasAttribute ('value'): opt. getAttributeNode ('value'). specified;
Data + = String. format ("{0} = {1} &", encoder (name), encoder (hasValue? Opt. value: opt. text ));
}
});
} Else if (! (/File | undefined | reset | button/I. test (type ))){
If (! (/Radio | checkbox/I. test (type )&&! Element. checked )&&! (Type = 'submit '& hasSubmit )){
Data + = encoder (name) + '=' + encoder (element. value) + '&';
HasSubmit =/submit/I. test (type );
}
}
}
});
Return data. substr (0, data. length-1 );
},

We can find that extjs uses the encodeURIComponent function in js to encode the value.
Is it the question of encodeURIComponent?
Search the network and find this problem mentioned in a blog post, see the http://www.yeahxj.com/2011/11/07/enter-code-for-all-browser/ (carriage return \ r \ n shameless performance in various browsers)
So I tested ie6, ie8, and ie9 respectively and found that this problem exists.
Summary:
This problem is small, but in some cases it may lead to people and headaches. This is also the sorrow of computer programmers, a large number of incompatibility, resulting in many programs have a variety of strange problems. For example, this small problem may cause a result. For example, in my program, you need to keep the carriage return line when the content is displayed on the foreground. There are two ways to deal with this problem. One is to use the <pre> label, but this label is inconvenient to use. Try to find that the format change is not well controlled. Another way is to change the carriage return line to the <br/> flag. I used this method. The regular expression I wrote at the beginning was/\ r \ n/, and I found nothing matched, later I will change it to/[\ r \ n]/, but the problem is that there will be an extra line in ie6, 7, and 8. Finally, for unified processing, I changed to/\ r \ n/combination, and then replaced/\ n /. The problem is solved.
** For original articles, please repeat the content of this section. Thank you very much!
** Microdreamsoft (Lin Shaohua ):
** Due to my limited level, you are welcome to correct me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.