Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Javascript to create .txt file? 2

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
Hi all. I'm not sure if this is even possible with javascript or not, but I thought I'd give it a shot...I am wanting to write a simple app that can be run locally on my computer. All I need it to do is take data from 3 seperate text boxes and join them together into a single dataset, then create/put them into a .txt file on my desktop. Any ideas/suggestions would be greatly appreciated. TIA-

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
You can achieve this, but this solution will be IE only. However, since you are just writing this up to save to your local pc I guess it won't be an issue because you know what your target audience will be.

You will have to use the ActiveX object - FileSystemObject. And then from there you will have to use the method .openTextFile to write to the file. There's an ok description here, but it's not perfect:


For a working example try out this:
Code:
<script type="text/javascript">
function blah() {
   var str = document.getElementById("txt").value;
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var newFile = fso.OpenTextFile("c:\\test.txt", 2, true, -2);
   newFile.WriteLine(str);
   newFile.Close();
}
</script>
<body>
<input type="text" id="txt"><br />
<input type="button" value="save text to file" onclick="blah()">
</body>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
images
 
Dan and Kaht,

Thanks for the suggestions. As javascript isn't really my thing, I'm sure I'll have plenty of other bonehead questions. I have just acquired a copy of the 'Javascript Bible', can you recommend any good web resources as well, other than tektips of course. Thanks again,

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
I have gotten my page to do everything I need it to do, with only one problem...When I ftp the created file onto a linux server the ' and " characters have been replaced with /x92 and /x93. My question: Could this be a js thing, or is it a linux/ftp client thing? If it is js, what can do to correct/prevent this, if not I am in the wrong forum.

TIA
Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Thanks again for the help. I have a simple followup:
How can I clear a textbox only once. Currently, I am using
Code:
function ClearInput(obj) {
obj.value = "";}

//with

<INPUT onclick="ClearInput(this)"; value=pc name=pc>
for all the textboxes/areas, and it works except once data has been entered, clicking on the input box will once again clear the text, which I dont want it to do. Thanks again

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
There are probably much better DOM methods to use for this, but I don't know any off the top of my head w/o searching on google. Nevertheless, this worked for me in IE:
Code:
<script type="text/javascript">
function blah(obj) {
   obj.value = "";
   obj.onfocus = "";
}
</script>
<input type="text" onfocus="blah(this)" value="test">

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Clearing the box is easy, the question is when do you want it to happen?
If you call your ClearInput function from an onclick event then it will happen every time you click on that field.

Under what circumstances do you want the field to clear?
After you click the save button and it saves the data?
The quickest way is to call your clear function after you have saved and closed the text file.
ClearInput('pc');
Passing in the name of the field to clear.

If you need to clear every field on the entire form their are scripts to do it. Different field types have to be cleared differently.

Or if you only need to reset the form back to it's defaults you can add a line: document.myformname.reset();
Or you can add a button:
<input type="reset" value="Reset">
and then click the button to reset.

It all depends on what you need to clear and when.


Stamp out, eliminate and abolish redundancy!
 
Here is a DOM method for clearing all form fields.
Did I leave any out?
Code:
function clearform(objForm) {
  var elArr = objForm.elements;
  for(var i=0; i<elArr.length; i++)
  {
    var fldtext = (elArr[i].type == 'select-one')?' value="'+(elArr[i].options(elArr[i].selectedIndex).text)+'"':'';
    if (elArr[i].type == 'text' || elArr[i].type == 'textarea')
      elArr[i].value = '';
    if (elArr[i].type == 'select-one' || elArr[i].type == 'select-multiple')
      elArr[i].selectedIndex = 0;
    if (elArr[i].type == 'checkbox' || elArr[i].type == 'radio')
      elArr[i].checked = false;
  }
}

Stamp out, eliminate and abolish redundancy!
 
Cool that helps..Here's what I came up with
Code:
function ClearInput(obj) {
if (obj.value=="pc"){
        obj.value = "";}
Which works since Im only dealing with 6 textboxes, so its not a huge deal to hand-code. But it strikes me as inefficient, I was wondering if there was some sort of pre-defined function. Thats cool tho, now I know about DOMs. I have a hard time even knowing what to search for since js lexicon is different than I'd expect.

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Here seems to be the DOM friendly version way to remove a handler:
Code:
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

I found this at the following address:


and other searches found more information on the methods used:

removeEventListener (Moz)

detachEvent (IE):

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Hi again, I didn't want to start a new thread since my question is still about the same project....Is it possible to open a page without a toolbar.. I know that this is simple when working with pop-up (child) windows. But I'm wondering how to do this with the parent (onLoad?). Keep in mind, this page isn't going on the net-it will only be run locally. TIA-

Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Cool that helps..Here's what I came up with
Code:
function ClearInput(obj) {
if (obj.value=="pc"){
        obj.value = "";}

Try this:
Code:
function ClearInput(obj) 
{
	if (obj.value==obj.[COLOR=red]defaultValue[/color])
	{
		obj.value = "";
	}
}

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Hi again, I didn't want to start a new thread since my question is still about the same project
Even so, it's better to simply ask a new question. That way, it's listed with it's own subject in the forum's thread list (rather than a comparatively obscure 15th post in a thread). It attracts more answers and will be easier for others to find using the search function.

As for the answer to your question, no, you cannot. The only way to do it is to open a new child window.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top