1
var userAgent = navigator.userAgent.toLowerCase();

    // Figure out what browser is being used.
    var Browser = {
        Version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
        Chrome: /chrome/.test(userAgent),
        Safari: /webkit/.test(userAgent),
        Opera: /opera/.test(userAgent),
        IE: /msie/.test(userAgent) && !/opera/.test(userAgent),
        Mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
        Check: function() { alert(userAgent); }
    };

    if (Browser.Chrome || Browser.Mozilla) {
        // A
    }
    else if (Browser.IE) {
        // B
    }
    else {
        // C
    }

So, suppose there is a javascript code like this in a HTML file. Can anyone show me how to print the result of this javascript code and write the result into a file in the server?

Also, what javascript codes would provide OS detection?

user1613156
  • 69
  • 2
  • 7

3 Answers3

2

To detect the operating system on the client machine, your script can analyze the value of navigator.appVersion or navigator.userAgent. Below is a simple example of a script that sets the variable OSName to reflect the actual client OS.

var OSName="Unknown OS";

if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

For writing to a file with js , There's been such questions on SO already, take a look at here : Writing to file

Though anyways, for security measures, JS won't allow you write to a file from the browser.

Community
  • 1
  • 1
SpiXel
  • 4,338
  • 1
  • 29
  • 45
1

JavaScript is client-side. It can't write to the server. It also can't access the client filesystem due to security restrictions.

Maybe start by asking why you want to acheive this - it sounds like there is a better approach.

Greg Owens
  • 3,878
  • 1
  • 18
  • 42
0

OS informations are stored in user agent too:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13

Windows NT 5.1 is Windows XP, language en-US