New line in browser (IE, Opera, Firefox)

I did a webpage before to store some message. The point is I need to count the total characters, including new line character.

In IE and Opera new line character will consume 2 spaces because they use the characters “\r\n”, however if the message is saved under Firefox 2 or Firefox 3, the new line character only consume 1 space, this will create a problem because the next program reading those message need the new line characters in format “\r\n”, means at least 2 spaces for that.

After some test I found that Firefox will use a single character “\n” to represent a new line character, it is different with IE and Opera which using “\r\n” as new line character.

This findings shows that message saved under firefox will has a little different because of that “\n”. In order to sync the saved output, I add some extra code to handle this situation for Firefox browser only:

// Handle Firefox save data issue
// (Firefox only use line-feed as new line)

if (browser == "Netscape") {
msg = msg.replace(/\n/g,"\r\n");

Above code will detect if there is a Firefox browser, and do some trick on new line character, replace “\n” to “\r\n” when save the message. So that the output will looks same in IE and Opera browser, and has no issue for next program to read.

I never test for Google Chrome and Safari, anybody who interest on that can test at your own. Do let me know your test result, 🙂

~~~ * ~~~ * ~~~

Update 14/Oct/2009

Thanks for yair comment. Basically this post is talking about line feed inside <textarea>, if you would like to show a line feed in website, <br /> would be your choice. 🙂

11 responses to “New line in browser (IE, Opera, Firefox)

  1. hi

    i tray to use

    if (browser == “Netscape”) {

    msg = msg.replace(/\n/g,”\r\n”);

    it not brake the line
    any idia

  2. Hi yair,

    Basically this article is talking about the line feed/carriege return in a html page text box (i.e. text inside <textarea>)

    The only way to show break the line in webpage is using <br> in your html code.

    Sorry if my article confuse you since I never state it clearly.


  3. Exactly what truly influenced you to publish “New line
    in browser (IE, Opera, Firefox) | 大脚板日记”?
    Ihonestly enjoyed reading it! Thanks for your effort -Linwood

  4. Excellent goods from you, man. I have understand your stuff
    previous to and you’re just too fantastic. I actually like what you have acquired here, certainly like what you’re saying and
    the way in which you say it. You make it entertaining and you still care
    for to keep it sensible. I can not wait to read far more from you.
    This is really a great site.

  5. Heloł,
    bardzo nietypowe miejsce, napewno chętnie odwiedze Cię tutaj nie raz, bardzo ciekawie piszesz twojego bloga – jest
    rzeczywiście bardzo ciekawy

  6. Dzień dobry,
    bardzo niezwykłe miejsce, napewno chętnie odwiedze Cię tu nie raz,
    bardzo ciekawie piszesz twojego bloga – jest niewątpliwie dużo

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s