Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6189377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:24:22+00:00 2026-05-24T02:24:22+00:00

\n or \r\n (or even \r ) in other words. I’m keen to avoid

  • 0

\n or \r\n (or even \r) in other words. I’m keen to avoid sniffing the user agent string.

First attempt:

var osLineBreak = (function () {
  var p = document.createElement('p');
  p.innerHTML = '<br>';
  return p.innerText;
}());

Unfortunately Firefox does not provide innerText, and textContent returns the empty string in this case.

Second attempt:

var osLineBreak = (function () {
  var
    lineBreak,
    body = document.body,
    range = document.createRange(),
    selection = window.getSelection(),
    p = document.createElement('p');

  // we cannot make a selection unless `p` is in the DOM,
  // so ensure that it is not visible when we insert it
  p.style.position = 'absolute';
  p.style.left = '-9999px';
  // Firefox returns the empty string if `innerHTML` is
  // set to "<br>", so include leading and trailing
  // characters
  p.innerHTML = '%<br>%';
  body.appendChild(p);
  // wrap `p` in `range`
  range.selectNodeContents(p);
  // make a selection from `range`
  selection.addRange(range);
  // see how the line break is treated in the selection
  // (provide a sane fallback in case we get the empty string)
  lineBreak = /%(.*)%/.exec(selection.toString())[1] || '\n';
  // revert our fiddlings
  selection.removeAllRanges();
  body.removeChild(p);
  return lineBreak;
}());

Is there a less convoluted technique that I’ve overlooked?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:24:23+00:00Added an answer on May 24, 2026 at 2:24 am

    Update in 2015: As you can see from the below, even back in 2011 Chrome and Firefox were using \n on all platforms. Opera and IE used \r\n on Windows. Since then, Opera has switched to WebKit like Chrome, and so presumably uses \n. IE8 was the last IE that used \r\n in textareas; from IE9 onward, IE also uses just \n. Haven’t tested mobile browsers.


    Coming rather late to the party, but if you really want to know what line separator character the browser is using in textareas and such (which can vary within browser even on the same OS), you can find out with a sneaky trick:

    function getLineBreakSequence() {
        var div, ta, text;
    
        div = document.createElement("div");
        div.innerHTML = "<textarea>one\ntwo</textarea>";
        ta = div.firstChild;
        text = ta.value;
        return text.indexOf("\r") >= 0 ? "\r\n" : "\n";
    }
    

    This works because the browsers that use \r\n convert the \n on-the-fly when parsing the HTML string. Here’s a live running copy you can try for yourself with your favorite browser(s). In IE and Opera (even Opera running on *nix), you’ll find it’s \r\n. On Chrome, Firefox, and Safari (even running on Windows), you’ll find it’s \n.

    That said, my experience is that inserting \n (e.g., when assigning to the value property, as opposed to the HTML string bit above) works even on browsers that would normally use \r\n. But that doesn’t mean there aren’t edge cases, and I have to admit I don’t do that (insert line breaks in textareas) with any kind of regularity, so if there are issues and I really should be using \r\n on some browsers, I may just have not found them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In other words could it be possible to create assembly, which does not even
I am working on this code but even after consulting other threads I am
Even though I've been reading other topics about my kind of problem, I can't
Galileo came out the other day, and even though plugins under Eclipse are, IMO,
I have two radio buttons next to each other, and even though each one
I can't seem to find this information in other questions, even though I think
The preprocessor can be used to replace certain keywords with other words using #define
I've been tasked with finding documents that contain certain words if other words exist
In other words, does it matter whether I use http://www.example.com/ or http://wwW.exAmPLe.COm/ ? I've
The goal is to implement a Gallery whose adapter returns ListViews (in other words,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.