Technical Site
Spiritual Site
Heart Beat Powered by ASP.net

Reading and Writin Files in JavaScript

The Following example shows how to read or write files in local file system using JavaScript. In this example a text file is created on loadng of page and then it is read back.

<html><head> 

      <SCRIPT LANGUAGE='JavaScript'> 
      var password='theirpassword'; 
      var name='username'; 

      function WriteToFile() { 
           var filename = 'g://JSFile.txt'; 
           var fso = new ActiveXObject('Scripting.FileSystemObject'); 
           if (fso.FileExists(filename)) { 
                var a, ForAppending, file; 
                ForAppending = 8; 
                file = fso.OpenTextFile(filename, ForAppending, false); 
                file.WriteLine(name); 
                file.WriteLine(password); 
                } 
           else { 
                var file = fso.CreateTextFile(filename, true); 
                file.WriteLine(password); 
                file.WriteLine(name); 
                } 
           file.Close(); 
           } 

      function ReadIt() { 
           var filename = 'g://HTML Tricks//JSFile.txt'
           if (confirm('Do you want to see what we put on your computer?')) { 
                var fso, a, ForReading; 
                ForReading = 1; 
                fso = new ActiveXObject('Scripting.FileSystemObject'); 
                file = fso.OpenTextFile(filename, ForReading, false); 
                var name = file.readline(); 
                var password = file.readline(); 
                file.Close(); 
                document.write(name + '<br>'); 
                document.write(password); 
           } 
      } 
      </SCRIPT> 
      </head> 
<body onload='WriteToFile();ReadIt()'>
</body> 
</html> 

Bookmark
del.icio.us digg Spurl.net Wists Simpy Newsvine BlinkList Furl reddit FARK BlogMarks Yahoo! My Web smarking Ma.gnolia Segnalo
Share