Wednesday, October 13, 2004

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

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.

Sunday, October 03, 2004

My approach to drop down menus using JavaScript

Drop down menu
<div>
<iframe></iframe>
<div></div>
</div>

Hide outer div by setting the Style.Visibility to "hidden".

Show it when you want to by setting Style.Visibility to "visible".

Be sure to set the z-index of the internal div higher than the z-index of the iframe; if you do not, then the iframe will show over the div instead of under. Div tags are windowed elements while iframes are considered windowless. That means that unless you set the z-index higher, the iframe will always show over the div.

For an example, please contact me until I have a link up for an example.