简体   繁体   English

如何使用Javascript在文本文件中写入值

[英]How to write value in text file using Javascript

I'm working on a number of views per page using JavaScript.我正在使用 JavaScript 处理每个页面的多个视图。

<SCRIPT LANGUAGE="JavaScript">
<!--
       var cookiec = document.cookie
       if (cookiec != "") {
               var eqchr = 0;
               for (var cloop = 1; cloop <= cookiec.length; cloop++) {
                       if (cookiec.charAt(cloop) == "=") {
                               eqchr=(++cloop);
                       }
               }
               var cookiess = 0;
               clength=cookiec.length;
               cookies="";
               for (cloop = eqchr; cloop < clength; cloop++) {
                       if (cookiec==";") {
                               cloop=clength;
                       }
                       else {
                               cookies = cookies + cookiec.charAt(cloop);                        
                       }
               }
               cookiess = parseInt(cookies);
               document.write("[" + cookiess + "]");
               cookiess++;
               cookies = cookiess;
               var one_week = 7 * 24 * 60 * 60 * 1000;
               var expDate = new Date();
               expDate.setTime(expDate.getTime() + one_week);
               document.cookie = "Counter=" + escape(cookies) + "; expires=" + expDate.toGMTString();
       }        
       else {
               var one_week = 7 * 24 * 60 * 60 * 1000;
               var expDate = new Date();
               expDate.setTime(expDate.getTime() + one_week);
                document.cookie = "Counter=2; expires=" + expDate.toGMTString();
               document.write("[1]");
       }
       
// -->
</SCRIPT>

I am using the above JavaScript to calculate the number of views per page and I want to write the data in a text file.我正在使用上面的 JavaScript 来计算每页的查看次数,并且我想将数据写入文本文件中。

Do you have any suggestions?你有什么建议吗?

If your JavaScript is running in a browser environment, I would highly recommend either using HTML5 localStorage for storing (key, value) pairs or using AJAX to communicate with a server instead of trying to access a file on the client machine which may potentially become a security/privacy issue.如果您的 JavaScript 在浏览器环境中运行,我强烈建议您使用HTML5 localStorage来存储(键、值)对或使用AJAX与服务器通信,而不是尝试访问客户端计算机上的文件,这可能会成为安全/隐私问题。 Below is a simple example of using localStorage to store a page view count:下面是一个使用localStorage存储页面查看计数的简单示例:

if (localStorage.numberOfViews) {
  localStorage.numberOfViews = Number(localStorage.numberOfViews) + 1;
} else {
  localStorage.numberOfViews = 1;
}

Hope this helps!希望这可以帮助!

Javascript, running in a normal web browser, has very very limited access to the local file system.运行在普通 Web 浏览器中的 Javascript 对本地文件系统的访问非常有限。

So modern web browsers will let you save data to a file in a specialized directory, isolated from everything else.因此,现代 Web 浏览器将允许您将数据保存到专用目录中的文件中,与其他任何内容隔离。

For the most part, using localStorage (as mentioned by the others), is your best bet.在大多数情况下,使用 localStorage(正如其他人提到的)是您最好的选择。

If you are running under Windows you can create a specialized file called an '.HTA' which runs with the same kind of access and permissions that regular files use.如果您在 Windows 下运行,您可以创建一个名为“.HTA”的专用文件,该文件以与常规文件使用的相同类型的访问和权限运行。

Attribute LANGUAGE="JavaScript" is deprecated.属性 LANGUAGE="JavaScript" 已弃用。 You can remove it.你可以删除它。 Now, replying your question, you can do it with PHP.现在,回答你的问题,你可以用 PHP 来做。 Send the data when the user enter the page, send it via AJAX to your server and proccess it with PHP.在用户进入页面时发送数据,通过 AJAX 将其发送到您的服务器并使用 PHP 进行处理。

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

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