Cvermule

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 13 October 2004

Can you use JavaScript to read and write to a file?

Posted on 12:11 by Unknown
Now I was searching high and low for the answer to this question. The big answer that I got was, JavaScript cannot read or write to files. However, it is possible to do this using ActiveX embedded into your JavaScript. The following is the code to do this.



//-------------------------------------------------------

function WriteToFile()

{

    var filename = "data.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(name);

        file.WriteLine(password);

    }

    file.Close();

}

//-------------------------------------------------------

function ReadFromFile()

{

    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();

}

//-------------------------------------------------------



In IE you can use the FileSystemObject to read and write to/from files.



You can get to a reference on how to do this all here.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

Categories

  • amazon
  • browser specific
  • coding
  • Eclipse
  • error
  • example
  • funny
  • google
  • grep
  • hacks
  • javascript
  • jott
  • music
  • null
  • phone
  • search
  • search engine
  • tip
  • tool
  • unix
  • web development
  • yui

Blog Archive

  • ►  2008 (5)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  January (1)
  • ►  2007 (6)
    • ►  December (1)
    • ►  September (1)
    • ►  July (1)
    • ►  June (1)
    • ►  March (2)
  • ►  2006 (10)
    • ►  November (1)
    • ►  August (1)
    • ►  May (2)
    • ►  April (4)
    • ►  March (2)
  • ►  2005 (10)
    • ►  August (3)
    • ►  June (2)
    • ►  February (5)
  • ▼  2004 (9)
    • ▼  October (2)
      • Can you use JavaScript to read and write to a file?
      • My approach to drop down menus using JavaScript
    • ►  September (7)
Powered by Blogger.

About Me

Unknown
View my complete profile