


JavaScript method to use FileSystemObject object to write the content of text file_javascript skills
本文实例讲述了JavaScript使用FileSystemObject对象写入文本文件内容的方法。分享给大家供大家参考。具体如下:
这段代码主要是练习JavaScript的FileSystemObject对象,用它来让JavaScript向一个文本文件中写入内容,这个TXT文件的路径你可以自己指定,然后定义好要写入的内容,运行代码,你会发现原来的TXT文本中的内容已改变。
运行效果如下图所示:
具体代码如下:
<html> <head> <title>写入文本文件</title> </head> <body> <hr> <script language="JavaScript"> var ForWriting = 2; var strFile = "c:\\test.txt"; var objFSO = new ActiveXObject("Scripting.FileSystemObject"); // 检查文件是否存在 if (objFSO.FileExists(strFile)){ // 打开文本文件 var objStream = objFSO.OpenTextFile(strFile,ForWriting,true,false); // 写入字符串数据 objStream.WriteLine("JavaScript的FileSystemObject对象"); objStream.WriteLine("JavaScript写入文本文件的内容"); document.write("写入文本文件" + strFile + "成功<br>"); objStream.Close(); // 关闭文件 } else document.write("文本文件: " + strFile + "不存在<br>"); </script> </body> </html>
但是有的情况下浏览器会报错未能创建对象.
F12开发者模式调试时发现:运行至new ActiveXObject这行时,automation服务器未能创建对象。
解决方法:
IE浏览器->工具->Internet选项->安全->自定义级别->设置
->“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”设置为启用,确定即可。
注意:
启用此选项只可用于调试本地代码,在访问其它网站前一定记得改过来。
否则恶意脚本将通过IE具有读、写、遍历你本地文件等的全部权限!
希望本文所述对大家的javascript程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Example of using OpenCSV to read and write CSV files in Java. CSV (Comma-SeparatedValues) refers to comma-separated values and is a common data storage format. In Java, OpenCSV is a commonly used tool library for reading and writing CSV files. This article will introduce how to use OpenCSV to implement examples of reading and writing CSV files. Introducing the OpenCSV library First, you need to introduce the OpenCSV library to

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
