简体   繁体   English

如何将html中的javascript结果写入文件

[英]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. 所以,假设在HTML文件中有这样的javascript代码。 Can anyone show me how to print the result of this javascript code and write the result into a file in the server? 谁能告诉我如何打印这个javascript代码的结果并将结果写入服务器中的文件?

Also, what javascript codes would provide OS detection? 此外,什么JavaScript代码将提供操作系统检测?

To detect the operating system on the client machine, your script can analyze the value of navigator.appVersion or navigator.userAgent. 要检测客户端计算机上的操作系统,您的脚本可以分析navigator.appVersion或navigator.userAgent的值。 Below is a simple example of a script that sets the variable OSName to reflect the actual client OS. 下面是一个脚本的简单示例,它设置变量OSName以反映实际的客户端操作系统。

var OSName="Unknown OS"; var OSName =“未知操作系统”;

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 对于使用js写入文件,已经有关于SO的问题了,请看这里: 写入文件

Though anyways, for security measures, JS won't allow you write to a file from the browser. 虽然无论如何,为了安全措施,JS不允许您从浏览器写入文件。

JavaScript is client-side. JavaScript是客户端的。 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 Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US)AppleWebKit / 525.13(KHTML,与Gecko一样)Chrome / 0.2.149.29 Safari / 525.13

Windows NT 5.1 is Windows XP, language en-US Windows NT 5.1是Windows XP,语言为en-US

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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