Background: I am trying to create a bookmarks page that will exist on a file server. I want to have a text file (containing my list of bookmarks) located in the same folder as the HTML file, but I also want users to be able to add bookmarks to that text file using an HTML form.
I am trying to append the text from the HTML form to the end of an existing text file in the same folder as my HTML file. The following code I have almost works, but it's creating a new file on my desktop instead of editing the existing file. Any ideas how I can fix this?
<head>
<title></title>
<script>
function Write(link, title)
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("links.txt", ForWriting, true);
f.Write(link + title);
f.Close();
}
</script>
</head>
<form onSubmit="Write(this['link'].value, this['title'].value)">
<input type="text" name="link" id="link" value="Link" size="20">
<input type="text" name="title" id="title" value="Title" size="20">
<input type="submit" value="Add Bookmark">
</form>
The code doesn't create a new file, unless the file doesn't exist. When you pass 2
as the second parameter to OpenTextFile
method, the file is opened for writing, i.e. Write
will replace all the text in the file with new content.
To fix this, you've to pass 8
as the second parameter to OpenTextFile
, that makes Write
to append the new content at the end of the file.