Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine OS newline in JavaScript

I am generating a file for the user to download, and I want to insert the correct newline character(s) for their platform (\n, \r, or \r\n).

I am aware of the following solutions, but none solve my problem exactly:

  • Querying navigator.platform or navigator.appVersion. These properties are deprecated, so I would prefer not to rely on them.
  • There are specific approaches for Firefox and NodeJS. These do not apply, as I'm creating a website, and I would prefer if it worked on all browsers.
  • There are ways to find the browser's newline characters, but I'm interested in the user's platform. (These are different: Firefox always uses \n, regardless of the OS.)
like image 806
Mangara Avatar asked Sep 14 '15 04:09

Mangara


People also ask

How do you know if a character is a newline?

Simply comparing to '\n' should solve your problem; depending on what you consider to be a newline character, you might also want to check for '\r' (carriage return).

How do you go to a new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

What line ending does JavaScript use?

On Unix machines, a single newline character 'n' does the job. On Macs, a carriage return 'r' is used. DOS and Windows use both: 'rn'.

What does \r do in JavaScript?

The RegExp \r Metacharacter in JavaScript is used to find the carriage return character (Carriage return means to return to the beginning of the current line without advancing downward). If it is found it returns the position else it returns -1.


2 Answers

After a little investigation, I can't find anywhere else than in the linked mdn article, it is said that navigator.platform is deprecated.

Both WHATWG specs and W3 specs still include it in their Living Standards and there is no note about deprecation.

Actually, the w3 working group asked in January to Firefox team to remove navigator.oscpu or to make it an alias to navigator.platform since the former is only implemented by this browser.

Note : The mdn page states that it is deprecated since September 2, 2014 but I'm not sure what motivated this edit though.

Anyway, I think that you can safely use this navigator.platform property to determine which OS the user is running on (bearing in mind that these informations can be spoofed by user, but then it's their problem isn't it?)

Edit

Got a response from @Teoli who made this edit back in 2014 :
Question :

Why navigator.platform is marked as deprecated on https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/platform … ?

Answer :

Because the spec asked browser vendor to put as few info in it to avoid fingerprinting. Putting '' would be spec. Don't rely on it.

Link to the mentioned specs request.

like image 184
Kaiido Avatar answered Oct 16 '22 19:10

Kaiido


Parsing navigator.userAgent might be a reasonably reliable way of detecting OS and hence which newline character to use.

Something along the lines of would probably do the trick - might need some tweaking on UA strings that are tested for the individual platforms.

Also it wouldn't work if the UA is spoofed.

function getLinebreak (){
    var linebreaks =  {
        Windows: "\r\n", 
        Mac: "\n", 
        Linux: "\n"
    }
    for(key in linebreaks){
        if(navigator.userAgent.indexOf(key) != -1){
            return linebreaks[key];
        }
    }
    return "\n"; 
}

Note: you could probably safely only check if it's Windows otherwise return \n in most cases

Something like this would likely work in most cases too:

function getLinebreak(){
  if(navigator.userAgent.indexOf("Windows") != -1){
    return "\r\n";
  }
  return "\n";
}
like image 26
Brian Avatar answered Oct 16 '22 17:10

Brian