开发者

what is the significance of carriage return in context of javascript-php?

开发者 https://www.devze.com 2023-01-25 04:26 出处:网络
I am entering data in the text field of my form that contains 2 new line character but no carriage return character. (I can confirm by strlen in js or using str.charCodeAt() function.)

I am entering data in the text field of my form that contains 2 new line character but no carriage return character. (I can confirm by strlen in js or using str.charCodeAt() function.)

Now I submit this form to a php script, which is somehow receiving a carriage return along with the new line character.

So basically, \n in js becomes \r\n in php.

I have 2 questions:

  1. Does anyone know why it could be happening ?
  2. Why is the carriage return really used for ?

Use case: form validation on server side and client side for limiting the number of characters for a fie开发者_Python百科ld.


JavaScript treats newlines as \n, however when you submit your form via HTTP, JavaScript has nothing to do with it (other than checking the text). Windows treats newlines as \r\n and so when you use Windows and hit the return key in a text box, it will actually enter a \r\n into the box. When you post this data to the server, JavaScript doesn't do this (unless you use Ajax), your browser does. Your browser will use the same setting for newlines as your OS and that's why you're seeing them in your PHP file but not in JavaScript. Different OS's treat newlines differently. See here for more background info.

Edit
Carriage return actually comes from old printer commands. A carriage return meant, "send the printer cartridge back to the beginning of the line" and a linefeed (or newline) meant, "go to the next line on the page."


The browser is replacing the \n with \r\n. CR these days is only needed for returning the cursor to the first column of the output console/terminal, and for legacy systems that can't deal with LF alone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号