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
ornavigator.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.)
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
ornavigator.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.)
- Possible duplicate of stackoverflow./questions/1155678/… – SameOldNick Commented Sep 14, 2015 at 4:13
- @ub3rst4r That question is about the browser's newline character, not the underlying platform. – Mangara Commented Sep 14, 2015 at 4:15
- @Kaiido That property seems to be supported only by Firefox. – Mangara Commented Sep 14, 2015 at 4:27
-
1
Actually, I can't find where else than in this mdn document it is said that
navigator.platform
is deprecated, it's not in the Living Standards – Kaiido Commented Sep 14, 2015 at 4:43 - Well to confirm this I should ask the mdn guys why they said so ( probably related to this proposal but can't be sure though.) – Kaiido Commented Sep 14, 2015 at 4:56
2 Answers
Reset to default 3After 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/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";
}