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 rn ).

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.)

  • 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.


    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: "rn", 
            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 "rn";
      }
      return "n";
    }
    
    链接地址: http://www.djcxy.com/p/87860.html

    上一篇: 如何在Swift 2 / AVPlayer中恢复背景音频?

    下一篇: 在JavaScript中确定OS换行符