简体   繁体   中英

how to write the result of javascript in html to a file

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM