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:
navigator.platform
or navigator.appVersion
. These properties are deprecated, so I would prefer not to rely on them.\n
, regardless of the OS.)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).
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.
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'.
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.
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?)
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.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With